0

I wounder how to make .ini file in Java. I know how to make .txt file, but how to make .ini file I don't. For reading and wrting I use ini4j lib and I thnik it works good. First I make some directory because of saving some data from user, then I want to make file and I get error java.io.FileNotFoundException for codeline ini.load(new FileReader(INI_PATH)); , that means that my code doesn't make .ini file in codeline File newFile = new File(newPath+"connect.ini"); . Please help me!

My code is:

 String path =System.getProperty("user.home");
 dir = new File(path+"/ProjectName");
 String newPath=path+"/ProjectName";
 if(dir.exists()){
System.out.println("DIRECTORY EXISTS");
 }
 else{
     dir.mkdir();
 }
 newPath=newPath+"/";
 File newFile = new File(newPath+"connect.ini");
 INI_PATH = newFile.getAbsolutePath();
 System.out.println("INI_PATH "+INI_PATH);
 Wini ini = new Wini();
 ini.load(new FileReader(INI_PATH));

 ...SOME CODE FOR ADDUING PAIRS....
  • You can find your answer from this post: http://stackoverflow.com/questions/193474/how-to-create-an-ini-file-to-store-some-settings-in-java – Prateek Sharma Dec 09 '13 at 20:53
  • I have read this post at lest 3 times, it says about making properties class and so on and I don't understand what actualy means ".myappdir" in first answer, which is most helpfull for my problem... – IndexOutOfDevelopersException Dec 09 '13 at 20:57
  • THere, simple approach is given, if you want to save properties, then use properties file instead of INI file. ".myappdir" is the directory where properties file will be saved. – Prateek Sharma Dec 09 '13 at 21:04

1 Answers1

1

You are getting a FileNotFoundException because the file does not exist on the disk, if you are trying to create the file in code use the following:

File newFile = new File(newPath+"connect.ini");
newFile.createNewFile();

the createNewFile() method on Java's File class will create a file if it doesn't exist, then you can feel free to use a FileReader or FileWriter to work with the newly created (but blank) file.

Stefan Nuxoll
  • 867
  • 5
  • 15
  • Ok this createNewFile is working, but now I have problems in line ini.store(); before that line I put some parameters like ini.put("DATABASE", "databasename","") and so on to save in ini file. The error is still FileNotFoundException. I have chechked that the file is in specified directory. Any suggestions? – IndexOutOfDevelopersException Dec 09 '13 at 21:46
  • We would need to see the relevant section of code before an answer for that issue specifically could be given. – Stefan Nuxoll Dec 09 '13 at 21:48