How to write a file in Java? To write a file in Java use the class FileWriter and BufferedWriter. The FileWriter class is used to write data into output stream. However, one can specify these values by using OutputStreamWriter on a FileOutputStream. There are various constructors of FileWriter class that allows data writing in output stream in various ways.