I\'m writing a program that writes sets of observations in the form of a String array (from User input) to file. I am able to write an observation to a .txt file and then ad
Sorry I can't comment Brian Agnew's answer because of my reputation, so I write it here.
Seems you never have any spaces in your array items, so you can successfully use them as a separators in your .txt file. When reading, just read the file line by line and separate items by split(" ") method.
bw.write(s);
bw.newLine();
bw.flush();
bw.write(s);
bw.write(System.getProperty("line.separator"));
bw.flush();
You need to push a line separator into the buffer.
newLine();
Here's the code
for(int i = 0; i < observation.length; i++) {
try (BufferedWriter bw
= new BufferedWriter(new FileWriter("birdobservations.txt", true))) {
String s;
s = observation[i];
bw.write(s);
bw.newLine();
bw.flush();
} catch(IOException ex) {
ex.printStackTrace();
}
}
Why not iterate within your try{}
block and use BufferedWriter.newLine() after each write ?
If you need to be able to read the values back in later, you need to consider some unambiguous output format. Perhaps the simplest solution is a CSV format (I note your output data has spaces - you would need to separate your entries using something other than spaces in that case)