0

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.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
user2791187
  • 5
  • 1
  • 1
  • 2
  • Where are you printing spaces and newlines? `write` only writes the string you give it. – Peter Lawrey Sep 18 '13 at 11:49
  • Don't flush after writing a couple of bytes! You're using a BufferedWriter that decides itself on when to flush. See here for example: [When to flush a BufferedWriter?](https://stackoverflow.com/a/908203/688182) – this Nov 10 '17 at 22:31

5 Answers5

8

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)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Ok, so .newLine seems to be correct...in a way... It separates each part of the array with a new line though, so maybe I should just leave it that way. Unfortunately we have to use a .txt file for the assignment. – user2791187 Sep 18 '13 at 11:57
  • Although you call it .txt, there's no reason why it can't contain comma separated values, is there ? – Brian Agnew Sep 18 '13 at 11:59
  • I guess I hadn't considered that. I'll look into it. Thanks! – user2791187 Sep 18 '13 at 19:35
3

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();
  }
} 
KiriSakow
  • 957
  • 1
  • 12
  • 22
Jabran Saeed
  • 5,760
  • 3
  • 21
  • 37
  • This answers the question perfectly. It might also be nice to iterate inside the try so you don't have to make a new BufferedWriter for every record. – JoshOrndorff Nov 13 '17 at 22:14
0
bw.write(s);
bw.write(System.getProperty("line.separator"));
bw.flush();
Viktor Ozerov
  • 295
  • 3
  • 10
0
 bw.write(s);
bw.newLine();
bw.flush();
loddn
  • 658
  • 1
  • 6
  • 16
0

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.

Community
  • 1
  • 1
Scadge
  • 9,380
  • 3
  • 30
  • 39