DEV Community

gladiator67
gladiator67

Posted on

java.io.FileNotFoundException: on Visual Studio Code maven project.

Hello! I try to create file with java.io.FileOutputStream on visual studio code. Below is the sample code.

File outputFile = new File("test.txt");
OutputStream os = new FileOutputStream(outputFile, true);
Enter fullscreen mode Exit fullscreen mode

But the code throws the java.io.FileNotFoundException exception. Before I use the visual studio code, I used the eclipse ide and this code works without any exception. Are there any options which give the file-creation-permission in visual studio code? For your information, my vs code is system installer version, not user installer.

Top comments (1)

Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi • Edited

You are trying to open a file from the a relative path (see: absolute vs relative path), more specifically you are trying to open a file from the current working directory. The current working directory might change depending on how you program is executed. You can see what is your current working directory with the following line:

System.out.println(new File("").getAbsolutePath());
Enter fullscreen mode Exit fullscreen mode

This should output the location of the current working directory. What you have to do is either move your file there in that directory or adjust the path in your new File("<adjust this>").