4

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();
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Alexis R Devitre
  • 288
  • 2
  • 6
  • 21
  • you should give full path / physical path of write location – Oomph Fortuity Jul 26 '14 at 09:28
  • the path that you are using looks like a relative path `"src/opinarium3/media/presentaciones/"+title+"/comments/"+Date.valueOf(LocalDate.now())+".txt"` The error is pretty obvious that it cannot find the file there. Try with absolute path or try putting a sample file on classpath and test with hard coded value for starter. – Pat Jul 26 '14 at 09:31

3 Answers3

5

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();
user253751
  • 57,427
  • 7
  • 48
  • 90
  • The source directory isn't there at runtime. – user207421 Jul 26 '14 at 09:39
  • @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 Jul 26 '14 at 09:56
  • @EJP, this will create an src directory in the current app context, so technically it answers OP's question. – svz Jul 26 '14 at 10:10
  • 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. – Alexis R Devitre Jul 26 '14 at 16:47
  • @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. – user253751 Jul 27 '14 at 20:55
4

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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • 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 :)! – Alexis R Devitre Jul 26 '14 at 10:00
1

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...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76