File Not Found Exception using Java FileWriter

13,795

Solution 1

Looking at

new FileWriter("src/opinarium3/media/presentaciones/"+title+"/comments/...")

I see that you are trying to introduce a directory from variable title. Not sure whether this creates all missing directories. So please make sure that this directory exists and create it before writing to the file two levels below.

Solution 2

new FileWriter will never create a directory. It will throw a FileNotFoundException if the directory doesn't exist.

To create the directory (and all parent directories that don't already exist), you can use something like this:

new File("src/opinarium3/media/presentaciones/"+title+"/comments/").mkdirs();

Solution 3

You can try any one based on file location. Just use prefix / to start looking into src folder.

// Read from resources folder parallel to src in your project
File file1 = new File("resources/abc.txt");
System.out.println(file1.getAbsolutePath());

// Read from src/resources folder
File file2 = new File(getClass().getResource("/resources/abc.txt").toURI());
System.out.println(file2.getAbsolutePath());

Note: Try to avoid spaces in the path.

First check whether folder(directory) exists or not:

sample code:

File f = new File("/Path/To/File/or/Directory");
if (f.exists() && f.isDirectory()) {
   ...
}

Read more...

Share:
13,795
Alexis R Devitre
Author by

Alexis R Devitre

B.Sc. Computer Sciences and Undergraduate student in Physics at Universidad de Costa Rica, San José, Costa Rica. Interested in data analysis, simulation and control codes for high energy physics.

Updated on June 25, 2022

Comments

  • Alexis R Devitre
    Alexis R Devitre almost 2 years

    I am trying to write to a txt file from a JAVA Application. I have tried using Buffered Writer, then just FileWriter to create a new file within a potentially new (not indefinitely since more files with different names will be later programatically written there by the same method) subfolder. I get the following error message (It's actually much longer but I think this is the key part of it):

    java.io.FileNotFoundException: src/opinarium3/media/presentaciones/Los fantasmas del Sistema Solar/comments/2014-07-26.txt (No such file or directory) at java.io.FileOutputStream.open(Native Method)

    And this is the code firing the issue (it activates as you press a button to register a comment having been filled in a custom form):

    private void fileCommentOnPresentation(String title, String positiveComments, String negativeComments, int grade) throws IOException{
        FileWriter bw;
        try{
            bw = new FileWriter("src/opinarium3/media/presentaciones/"+title+"/comments/"+Date.valueOf(LocalDate.now())+".txt");
            bw.write(title+"\n"+positiveComments+"\n"+negativeComments+"\n"+grade);
            bw.flush();
            bw.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
  • user207421
    user207421 almost 10 years
    The source directory isn't there at runtime.
  • Alexis R Devitre
    Alexis R Devitre almost 10 years
    @EJP I have been able to read things from there at runtime but I guess it's only because the compiler copies those file somewhere into the Jar or something like that... Is it possible to have a directory which can be guaranteed to stay relative to the the DIST so that i can both read and write from it at runtime??
  • Alexis R Devitre
    Alexis R Devitre almost 10 years
    It does! But at an even lower level I am in fact trying to create a directory dynamically so I'm starting to get a sense of what is wrong here, thanks :)!
  • svz
    svz almost 10 years
    @EJP, this will create an src directory in the current app context, so technically it answers OP's question.
  • Alexis R Devitre
    Alexis R Devitre almost 10 years
    Right it should create a directory called src even though it's not the same as the original source, those observations are still very valuable to me since i am trying to use the folder as some kind of super-simplified-no-need-to-be-secure database.
  • user253751
    user253751 almost 10 years
    @AlexisRustamDevitre be careful with that. Eventually you might let someone else access your application, and then it does need to be secure, but you might forget about all the places where it's insecure and forget to fix one.