0

I'm working on a school project where I need to initialize the state of my program using a txt file. I have never really worked with reading files so I used the tutorial here to generate the path name of the resources folder used with the project.

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("savedState.txt").toURI());
BufferedReader br = new BufferedReader(new FileReader(file));

This works great! The trouble comes in when I try to write back to the file. I've tried using a similar approach and it doesn't seem to work. The file remains empty when I use this code.

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("savedState.txt").toURI());
//File file = new File("/actual file path/savedState.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));

The darndest thing is that it works fine when I use the actual file path (the third line instead of the first two). I'm not sure how to make it work!

I guess I should also add what is being written (maybe I'm doing that wrong). This is what it is working on

for (int i=0;i<tokens.size();i++)
    bw.write(tokens.get(i)+',');

bw.close();

tokens is an ArrayList, type String

Paul Myers
  • 41
  • 1
  • 1
  • 6
  • 1
    Did you close the writer when finished? If not, try `bw.close();`. Being buffered, bytes are not necessarily written to disk when you write to it; they’re written when the buffer is full, and when you flush or close it. – Bohemian Jun 03 '18 at 21:02
  • 1
    If the file is a regular file on disk, why are you using class loader’s getResource? Why not just use a relative file path? getResource is usually (always?) used with files inside jars, which btw can’t be written to. – Bohemian Jun 03 '18 at 21:13
  • Is this somehow a duplicate of https://stackoverflow.com/questions/13000937/read-and-write-to-java-file-via-resource ? – Michael Beer Jun 03 '18 at 21:17

3 Answers3

0

Try:

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
Zander
  • 84
  • 14
  • You could try to move the .txt to the same folder as the program and then just put "avedState.txt" in where 'file' is. Also try pw.flush() – Zander Jun 03 '18 at 21:08
  • I tried moving file and using new File("savedState.txt") but that's not working either – Paul Myers Jun 03 '18 at 21:21
0

The third line, the one which works, should be the most correct one if you want to WRITE file. As an example, if the file is inside your .jar file, or .war file, you will be able to read it using the Classloader, but not write to it.

Suggestion: Call your program like this:

java -Dfilepath=/path/to/file -jar yourjar.jar

Then in your code:

File file = new File(System.getProperty("filepath"));
Guilherme Mussi
  • 956
  • 7
  • 14
0

Guilherme Mussi informed me that what I was doing probably wouldn't work for writing. So I did more looking and found that constructing the file path wasn't so hard. Here is the code I ended up with and it works for both reading and writing.

String fsep = System.getProperty("file.separator");
File file = new File(System.getProperty("user.dir")+
        fsep+"src"+fsep+"main"+fsep+"resources"+fsep+"savedState.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
Paul Myers
  • 41
  • 1
  • 1
  • 6
  • The problem with this is once you’ve built the jar, the path won’t work. The answer is as per my comment above: you can read from resources built into your jar, but you can’t write to them. When you deploy and run your app, there won’t be a src directory. – Bohemian Jun 04 '18 at 00:20