Home Java 8 Ways to Write to File in Java

8 Ways to Write to File in Java

We’ll look at many methods for writing to a file in Java. These include BufferedWriter, PrintWriter, FileOutputStream, DataOutputStream, RandomAccessFile, FileChannel, and the Java 7 Files utility class used in this example. We’ll also discuss locking the file while writing and several other aspects of writing to file.

Write using PrintWriter

The example below looks at how to use PrintWriter to save formatted text to a file:

public void writeWithPrintWriter()
  throws IOException {
    FileWriter fWriter = new FileWriter(fileName);
    PrintWriter pWriter = new PrintWriter(fWriter);
    pWriter.print("Some String");
    pWriter.printf("The grocery name is %s and its price is %d $", "Tomatoes", 250);

With the printf method, we’re publishing a raw String to a file and some prepared text. We can use FileWriter, BufferedWriter, or even System.out to generate the writer.

How to Write with FileChannel

FileChannel can be faster than regular IO when dealing with huge files. Using FileChannel, the following code writes String to a file:

public void writeUsingFileChannel()
  throws IOException {
    RandomAccessFile raStream = new RandomAccessFile(fileName, "rw");
    FileChannel fileChannel = raStream.getChannel();
    String strVal = "Codeunderscored";
    byte[] stringBytes = strVal.getBytes();
    ByteBuffer byteBuffer = ByteBuffer.allocate(stringBytes .length);

    // verification
    RandomAccessFile raFile = new RandomAccessFile(fileName, "r");
    assertEquals(strVal, raFile.readLine());

How to Write with FileOutputStream

Now let’s look at how we can send binary data to a file using FileOutputStream. The following code transforms a String to bytes and uses FileOutputStream to write the bytes to a file:

public void writeUsingFileOutputStream()
  throws IOException {
    String strVar = "Codeunderscored";
    FileOutputStream outputStream = new FileOutputStream(fileName);
    byte[] strBytes = strVar.getBytes();


How to Write with DataOutputStream

Next, let’s look at how to write a String to a file using DataOutputStream.

public void writeUsingDataOutputStream()
  throws IOException {
    String value = "Codeundersocred";
    FileOutputStream foStream = new FileOutputStream(fileName);
    DataOutputStream doStream = new DataOutputStream(new BufferedOutputStream(foStream));

    // results verification
    String result;
    FileInputStream fiStream = new FileInputStream(fileName);
    DataInputStream diStream = new DataInputStream(fiStream);
    result = diStream.readUTF();

    assertEquals(value, result);

How to Write with BufferedWriter

Let’s start easy by writing a String to a new file with BufferedWriter:

public void WriteUsingBufferedWritter()
  throws IOException {
    String strVar = "Codeunderscored";
    BufferedWriter bfWriter = new BufferedWriter(new FileWriter(fileName));

Subsequently, we can append a String to the existing file as follows:

public void appendStringUsingBufferedWritter()
  throws IOException {
    String strVar = "underscored";
    BufferedWriter brWriter = new BufferedWriter(new FileWriter(fileName, true));
    brWriter.append(' ');
    brWriter.append(strVar );

How to Write to a Temporary File

Let’s try writing to a temporary file now. The code below makes a temporary file and writes a String to it:

public void writeToTemporaryFile() throws IOException {
    String strWrite = "Codeunderscored";
    File temporaryFile = File.createTempFile("trial", ".tmp");
    FileWriter fWriter = new FileWriter(temporaryFile);

    BufferedReader bReader = new BufferedReader(new FileReader(temporaryFile));
    assertEquals(strWrite, bReader.readLine());

As we can see, the intriguing and unique part is the generation of the temporary file. After that, we write to the file the same as before.

How to Write with RandomAccessFile

Let’s look at writing and editing within an existing file rather than creating a new one or adding to one that already exists. , we require unrestricted access. RandomAccessFile allows us to write to a specific location in a file based on bytes offset from the file’s beginning. This code creates an integer value with an offset from the file’s start:

private void writeToPosition(String fname, int data, long pos)
  throws IOException {
    RandomAccessFile  raFile = new RandomAccessFile(fname, "rw");

This method is used to read the int stored at a given location:

private int readFromPosition(String fname, long pos)
  throws IOException {
    int result = 0;
    RandomAccessFile raFile = new RandomAccessFile(fname, "r");
    resultVals = reader.readInt();
    return resultVals;

Let’s write an integer, change it, and then read it back to see how well our functions work:

public void writingToSpecificFilePosition()
  throws IOException {
    int yearData = 2022;
    int countData = 4000;
    writeToPosition(fileName, yearData, 10);
    assertEquals(yearData, readFromPosition(fileName, 10));
    writeToPosition(fileName2, countData, 10);
    assertEquals(countData, readFromPosition(fileName, 10));

How to Write with Files Class

A new utility class in Java 7, Files, gives interfacing with the filesystem. We may create, move, copy, and remove files and directories using the Files class. It can also read and write data to a file:

public void writeWithFileClass()

  throws IOException {
    String strVal = "Codeunderscored";

    Path fetchPath = Paths.get(fileName);
    byte[] strBytes = strVal.getBytes();

    Files.write(fetchPath, strBytes);

    String readString = Files.readAllLines(fetchPath).get(0);
    assertEquals(strVal, readString);

How to Lock File Before Writing

Finally, when writing to a file, we must occasionally double-check that no one else is doing so simultaneously. Essentially, we need to lock the file while it is being written. Let’s utilize FileChannel to see if we can lock the file before writing to it:

public void LockFileBeforeWriting()
  throws IOException {
    RandomAccessFile raFile = new RandomAccessFile(fileName, "rw");
    FileChannel fChannel = raFile.getChannel();

    FileLock fileLock = null;
    try {
        lock = fChannel.tryLock();
    } catch (final OverlappingFileLockException e) {
    stream.writeChars("Test drive lock");


An OverlappingFileLockException will be triggered if the file is already locked when we try to acquire the lock.


This article has demonstrated how to write data to a file in Java using various methods. However, the following are the key takeaways from us.

A FileNotFoundException will be issued if we try to read from a file that does not exist. In addition, if we try to write to an empty file, the file is created first, and no exception is issued. Because the stream is not closed implicitly, it is critical to end it after each use to relinquish associated resources. Before abandoning the resources in the output stream, the close() function calls flush(), which forces any buffered bytes to be sent to the stream.

Although several of these classes’ APIs allow for more, this is an excellent place to start. PrintWriter is used to write formatted text, FileOutputStream is used to write binary data, while the DataOutputStream is used to write basic data types. In addition, the RandomAccessFile is used to write to a specific point, and FileChannel is used to write quicker in larger files.

You may also like

Leave a Comment