2

In the following code, I first created a File in the root directory and then write string into it. Then I read data from that File:

        File file = new File("myfile.txt");
            if(!file.exists()){
                file.createNewFile();
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(com_port_hardcoded);
                bw.close();
            }
            Scanner scn = new Scanner(file);
            comPort = scn.nextLine().trim();
            scn.close();

Now, when I compile and run this code in eclipse, it works perfectly. But when I export a 'runnable jar' of this code, the txt file is not there in the folder of the jar.

I don't know where the txt file is generated or if it is generated at all.

Vishal
  • 33
  • 1
  • 2
  • 7

1 Answers1

1

If you want the generated file locate in the same jar folder, you can do this:

File file = new File(System.getProperty("user.dir"), "myfile.txt");
Salah
  • 8,567
  • 3
  • 26
  • 43