Meenakshi
Meenakshi

Reputation: 468

File not found exception while reading a property file

 public boolean  WriteProperty(String key,String value){
            try{  
                    int check=0;
                    while(check == 0){
                check=1;

                Properties pro = new Properties();

                File f = new File("/properties/IxFbConfiguration.properties");
                if(!f.exists()){
                  check=0;
                  System.out.println("File not found!");
                }
                else{
                  FileInputStream in = new FileInputStream(f);
                  pro.load(in);
                  System.out.print("Enter Key : ");
                  System.out.print("Enter Value : ");
                  pro.setProperty(key, value);

                  System.out.println("the property is"+pro.getProperty(key));
                 // pro.store(new FileOutputStream(str + ".properties"),null);
                  pro.store(new FileOutputStream("/properties/IxFbConfiguration.properties"),null);
                  System.out.println("Operation completly successfuly!");
                }
              }
            }
            catch(IOException e){
            System.out.println(e.getMessage());
            }
            return false;
          }

I get file not found exception when I run this code.

I do have a folder properties which contains the IxFbConfiguration.properties file. When I hardcode the full path as C:\Documents and Settings\meenakshib.DCKAP-066\Desktop\xblitzjApril18\properties\IxFbConfiguration.properties it works .

But I have a problem when i use the jar. I tried using

 getClass().getResourceAsStream("/properties/IxFbConfiguration.properties")

also but it says path not recognised.

Upvotes: 0

Views: 3419

Answers (1)

Samuel EUSTACHI
Samuel EUSTACHI

Reputation: 3156

using new File(....) with a static values (path) , IMHO, is not a correct way to access a file

If you want this code to work on your IDE, AND on your server, AND for different environments, the path of the file should be :

either configurable, and an absolute path (C:/ etc on a windows env.)

or, in the classpath

Upvotes: 1

Related Questions