3

I am new to File Streams and would appreciate some help. The following code is the code I use to write to a specified file.

OutputStream outStream = new FileOutputStream(file);
outStream.write(contentsToWrite.getBytes());
outStream.close();
  1. How do I save different lines to a file? In my case using \n does not work when writing to a file.
  2. How do I save a line to the file without deleting the other lines?
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate [Write multiple lines in a text file ( java)](https://stackoverflow.com/questions/21813434/write-multiple-lines-in-a-text-file-java) and/or [How to append text to an existing file in Java](https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) note- if you want a new line between the new and old content, you may need prefix the new content with a [`System.lineSeparator()`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#lineSeparator--) – MadProgrammer Jul 20 '18 at 01:22
  • 1
    what editor are you using to open the file? If the file is using Unix line ending then it can be viewed by any thing other than Notepad, or the newer version of Notepad in Windows 10 – phuclv Jul 20 '18 at 01:32
  • Never use `String.getBytes()`, always use one of the `getBytes` that accepts a character set or character set name. However in this case, you would be better off using a (buffered) writer than an `OutputStream`. – Mark Rotteveel Jul 21 '18 at 11:55

3 Answers3

4

There is a nice, simple method which allows you to do this with a List of Strings you want to write and the file itself.

        List<String> lines=new ArrayList<>(contentToWrite);//if it is an array or something that isn't a list
        Files.write(file.toPath(),lines);
Ryan_DS
  • 750
  • 7
  • 28
2

Java has some wrapper class to file streams. BufferedWriter can be used to write string to file.

boolean append = true;
String filename = "/path/to/file";

BufferedWriter writer = new BufferedWriter(new FileWriter(filename, append));
// OR: BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename, append)));

writer.write(line1);
writer.newLine();
writer.write(line2);
writer.newLine();
// ......       

writer.close();

append meanings you write to the end of the file instead of empty the file.

zhh
  • 2,346
  • 1
  • 11
  • 22
  • 1
    A(nother) great advantage of using `BufferedWriter` is that it will supply the correct end-of-line character for the system on which it's running, as opposed to some hard coded (non cross-platform) EOL like `\n`.. – Andrew Thompson Jul 20 '18 at 04:27
  • Using `FileWriter` has the downside that it will always use the default character encoding. – Mark Rotteveel Jul 21 '18 at 11:56
0

The FileOutputStream class constructor method has the second parameter. if you set it to true. It will append the content the file you write. And "\r\n" can change to a new line.

OutputStream outStream = new FileOutputStream("a.txt",true);
outStream.write("hello".getBytes());
outStream.write("\r\n".getBytes());
outStream.write("hello".getBytes());
outStream.close();
Jswq
  • 758
  • 1
  • 7
  • 23