Writing a string array to file using Java - separate lines.
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 add a new observation without removing the
previous data, but all my data is on the same line. I need each set of
observations to be on a separate line. Additionally I will need to be able
to access the file later on and read from it.
My code currently looks like this: (observation is the string array)
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.flush();
}
catch(IOException ex) {
}
My output currently looks like this:
CrowMOsloMay2015JayMOsloJune2012CrowMOsloMay2015RobinFBergenMay2012
I would like it to look like this:
Crow M Oslo May2015
Jay M Oslo June2012 ...etc
How do I do this? I assume I need some kind of loop, but I've been stuck
on figuring this out for a while now.
No comments:
Post a Comment