-3

I have a property file and i want to read it in my java program.. The code is

Properties prop = new Properties();
File file =new  File("sendmails.properties");
String absolutePath = file.getAbsolutePath();


FileReader reader = new FileReader(file) ; 
prop.load(reader);
  • File not found exception shows.
  • Both files are in same directory

Can anyone give me a solution?

Parth
  • 141
  • 1
  • 2
  • 14
  • What IDE do you use? Where do you put `sendmails.properties`? Show your project structure. – Mikita Belahlazau Feb 07 '13 at 07:31
  • 2
    Well, as the exception says - it can't find the file. We can't really help you without knowing why you *expect* it to be found. Where is the file, and where is your code running? – Jon Skeet Feb 07 '13 at 07:35

6 Answers6

0
File file = new File("sendmails.properties");
                   ^^^^^   

Place put correct file path.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

I guess the problem is your working directory.

You can check the working dir with:

String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);

And check if the file exists under this folder (I guess not...)

Two solutions:

  1. Use absolute path, as you can get from file system

    File file =new File("d:/sendmails.properties");

  2. Check for the relative path from the current working dir. If you need to go 4 folders up use:

    File file =new File("../../../../sendmails.properties");

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
  • the absolute path getting correctely.. but the file not found exception in the FileReader reader = new FileReader(file) ; – user2049787 Feb 07 '13 at 07:57
  • When you get the absolute path, go to file system and check if that absolute path really points to where the file is (my guess it's not there). Than you can use one of the two solutions. – BobTheBuilder Feb 07 '13 at 08:04
  • String absolutePath = file.getAbsolutePath() give the correct location – user2049787 Feb 07 '13 at 08:41
  • FileReader reader = new FileReader(file) ; is it correct??? file gives the file name only – user2049787 Feb 07 '13 at 08:41
  • What do you mean "file gives the file name only"? It looks OK. Try using the absolute path from file system, in: file =new File("d:/sendmails.properties"); If all doesn't work, It might be related to file permissions. – BobTheBuilder Feb 07 '13 at 08:47
  • when giving the path ("d:/sendmails.properties") works properly but giving the path name as static gives error if a user save the property file in c:/ or any other location – user2049787 Feb 07 '13 at 08:56
  • Now I'm starting to understand. In order to get the file, you must know where it is saved. You can do it if you save the file in the same place for every user (or a specific place for every user, that you can get somehow) or if you are able to know the full path of the file once the user saved the file. You cant get a file if you don't know where it is stored. – BobTheBuilder Feb 07 '13 at 09:00
  • if i give the file name as static / is used as file seperator..But value getting frm getAbsolutePath() is D:/sendmails.properties . "/"must get as file seperator . can i?? – user2049787 Feb 07 '13 at 09:09
  • sorry "D:\sendmail.properties" is the value getting fom getAbsolitePath – user2049787 Feb 07 '13 at 09:11
  • Look at [this](http://stackoverflow.com/a/2417546/1651233) for understanding file separator.What you used is OK. But the main issue is how to know where the user saved the file. – BobTheBuilder Feb 07 '13 at 09:14
  • sorry ..i dont get the result – user2049787 Feb 07 '13 at 10:03
  • How about adding file path argument when running the java code? – BobTheBuilder Feb 07 '13 at 10:18
  • FileReader reader = new FileReader("D:/Hiringsteps_phase2/hiringsteps/src/main/resources/property/sendmails.properties"); is working good.. My question is how can read path of file without using the static path – user2049787 Feb 07 '13 at 10:36
0

When creating the file, either use the absolute path, or have the properties in your classpath and load them with the class loader:

prop.load(classLoader.getResourceAsStream("sendmails.properties"))
Amir Kost
  • 2,148
  • 1
  • 16
  • 30
0

The properties file should be in your classpath. Add the directory where the properties file is located into your server/system classpath and your problem shall be resolved.

jsjunkie
  • 559
  • 3
  • 7
0
package readFile;
import java.util.Enumeration;
import java.util.ResourceBundle;
public class ReadPropFile 
{
    public static void main(String[] args) 
    {
        ResourceBundle bundle = ResourceBundle.getBundle("MyProp");
        //System.out.println(bundle.toString());
        String email = bundle.getString("email");
        String password = bundle.getString("password");
        System.out.println("Email :: "+email);
        System.out.println("Password :: "+password);
        // Fetch all the Properties.
        Enumeration keys = bundle.getKeys();
        while(keys.hasMoreElements()){
        System.out.println(keys.nextElement());
        }
    }
}

MyProp.properties should be in src folder

MyProp.properties email = hussain password = hussain

0

What I did was:

  Properties props = new PropertiesBean(configPath);
    /* where 
    configPath = this.getClass().getName();
    */

Hope that helps.

sa_nyc
  • 971
  • 1
  • 13
  • 23