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