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?
3
  • Possible duplicate Write multiple lines in a text file ( java) and/or 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() Commented Jul 20, 2018 at 1: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 Commented Jul 20, 2018 at 1: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. Commented Jul 21, 2018 at 11:55

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

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..
Using FileWriter has the downside that it will always use the default character encoding.
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();

3 Comments

Thank you for the help. What difference does it make when it appends to the document?
@CameronReynes append means adding the new lines after the existed
save a line to the file everytime without deleting the existed lines