Trouble with filewriter overwriting files instead of appending to the end
Asked Answered
L

3

14

OK, I'm having some trouble writing multiple lines to a text file.

the program runs, but it won't use new lines each time

when I want it run 4 times, the text file should look like:

a
b
c
d

instead, it looks like:

d

who knows how to fix this problem? all imports are correctly imported.

source(it's been slightly edited, assume everything is properly defined):

import java.io.*;
public class Compiler {
public static void main (String args[]) throws IOException
{
    //there's lots of code here
    BufferedWriter outStream= new BufferedWriter(new FileWriter("output.txt"));
    outStream.newLine();
    outStream.write(output);
    outStream.close();
}

}
Limbate answered 29/5, 2012 at 17:58 Comment(2)
Just a warning, but this is dangerously close to a "what's the code" question. However, since you didn't simply ask for help (you showed what you've done so far), I'll turn a blind eye.Rhebarhee
output isn't defined, so this obviously won't compile. I assume this runs 4 times, each with output set to the subsequent letter in the alphabet. Obviously the solution would be massively different if "a\nb\n\c\nd\n" is the value of output.Kelpie
I
26

Make sure that when you create an instance of a FileWriter, that you are appending to the end of it. This can be done by using this specific FileWriter constructor which takes an additional boolean as a second parameter. This boolean tells the FileWriter to append to the end of the file, rather than overwriting the file.

BufferedWriter outStream= new BufferedWriter(new FileWriter("encoded.txt", true));
Invulnerable answered 29/5, 2012 at 18:2 Comment(4)
I'm a little new to the inputs and outputs of java, so could I get an example? I'm not in the ideal place for testing right now (no JDK or IDE's on this workstation)Limbate
My answer already provides the changes you should apply to make this work.Invulnerable
I wrote that when there was no code in it. Thank you for the answer.Limbate
I am using 2nd parameter as true and during the same run of the application it appends nicely. But when I start same application again (with 2nd param as true), it destroys all info of the previous run session and then again nicely appending... While i try to implement my own primitive logger and wish to keep in log all the info... Any clues?Yeomanry
R
6

By default FileWriter will overwrite the file. What you might want to do is define the reader in the following manner:
new FileWriter("encoded.txt", true)
This way the file will be appended to instead of being overwritten.

Hope this helps!

Rhebarhee answered 29/5, 2012 at 18:4 Comment(0)
D
0

I'm not sure what this code is supposed to do. It throws an error if your input string is more than one character long, because you close your FileWriter inside the loop, then try to write to it again.

I'm interpreting your question the following way: you're wondering why only the most recent output is in the file. In that case, it's because you didn't create your FileWriter in append mode. Look at the different constructors available for FileWriter, and use the one that allows you to append to the file.

Denverdeny answered 29/5, 2012 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.