-1

I have this method that writes to a file every time it's called:

public void writeToFile(String ins) {
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);

    bw.write(ins);
    bw.newLine();

    bw.close();
    fw.close();
}

But it only writes on the very first line of the file.

So, if I called it once with "Hello" and then again with "World", the file would contain "World", but the result I'm looking for is:

Hello

World

I tried using BufferedWriter.newLine() before and after writing the string but the result is the same?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 2
    Did you check the documentation for `FileWriter`? Check out the second argument to the constructor: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean) (In other words, you want to `append` to a file, not write/overwrite it) – domsson May 08 '17 at 13:38
  • 2
    Possible duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Tom May 08 '17 at 13:39
  • http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – AppX May 08 '17 at 13:40
  • Beyond that: you got various answers by now; please pick one to accept. – GhostCat May 09 '17 at 06:49

4 Answers4

3

read the documentation of FileWriter:

FileWriter(File file, boolean append) Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

and you see, that you need to set the append value to true:

FileWriter fw = new FileWriter(f, true);
Jens
  • 67,715
  • 15
  • 98
  • 113
3

You have to use FileWriter(String fileName, boolean append)

FileWriter fw = new FileWriter(f, true);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

The point is: your code does what it is supposed to do - it uses a FileWriter, which by default will create a new, empty file; it writes one string; and closes the FileWriter.

If you want to write more than one line; you either have to

  • use the FileWriter in APPEND mode when doing later writes (by using that second, boolean argument for the FileWriter constructor with true)
  • change your method to take a list of strings, and write all of them at once
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I like that statement : "your code does what it is supposed to do" It is usually true, but the dev don't code what it is supposed to be done ;) – AxelH May 08 '17 at 13:51
2

you can use a escape character:

\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.

I recommend you to use resource try to allow java to close the file when it will necessary

public void writeToFile(String ins) {

    String fileName= "file.txt";

    try (FileWriter fileWritter = new FileWriter(fileName, true)) {

        fileWritter.write(ins + "\r\n");

    } catch (IOException ex) {

    }
}

give to this method a empty string "" to insert in the file a "Enter"

MaQuiNa1995
  • 431
  • 1
  • 9
  • 23