Last Updated:

Write to a file in Java using FileWriter, BufferedWriter

How to write to a file. Theory

A FileWriter is the easiest way to write information to a file. It provides an overloaded method for writing an int, a byte array, and a String to a file. FileWriter is great for capturing small amounts of data.write()

BufferedWriter is almost identical to FileWriter, but uses a buffer internally to write data to a file. If you need to write large amounts of information to a file, then your choice should fall on BufferedWriter.

FileWriter и BufferedWriter they do a good job of writing text information to a file, but when the task is to write stream data to a file, it is advisable to use FileOutputStream.

Files is a helper class for working with files. It contains a method that it uses internally to write a byte array to a file.OutputStream

Write to a file in Java. Practice

Below is a program for writing information to a file using classes and .FileWriter, BufferedWriter, FileOutputStreamFiles

Note that in the method of writing to a file using we used the file path separator. Read more about it here.BufferedWriter"line.separator"

These are standard methods of writing data to a Java file. Use the right method, starting from the features of your project. For more information about working with files, see the Java IO section.