Java has multiple methods to create a file. In the given example, we opened a new file for writing the content. This tutorial discusses methods to create a text file and write data to it in Java. Example 1: Creating a new file and Writing to it using FileWriter. In each example, we will write the file dataOut.txt with the content 'humpty dumpty'. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here the major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.įollowing example, which makes the use of these two classes to copy an input file (having unicode characters) into an output file − Example import java.io. In particular, the most important part of the solution is to invoke the FileWriter constructor in the proper manner. Lets see a few examples of writing to a file using the FileWriter in Java. Though there are many classes related to character streams but the most frequently used classes are, FileReader and FileWriter. Java Byte streams are used to perform input and output of 8-bit bytes, whereas Java Character streams are used to perform input and output for 16-bit unicode. This example demonstrates the way of creating a new file by using File() constructor and file.createNewFile() method of File class.