WikiGalaxy

Personalize

Java Create/Write Files

Using FileWriter:

The FileWriter class in Java is used to write character-oriented data to a file. It is a subclass of OutputStreamWriter and is intended for writing streams of characters.


import java.io.FileWriter;
import java.io.IOException;

class WriteToFile {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("output.txt");
            writer.write("Hello, World!");
            writer.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file.

Using BufferedWriter

BufferedWriter Example:

The BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It inherits Writer class.


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

class BufferedWriteExample {
    public static void main(String[] args) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("bufferedOutput.txt"));
            writer.write("Buffered writing to a file.");
            writer.close();
            System.out.println("Successfully wrote to the file using BufferedWriter.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using BufferedWriter.

Using PrintWriter

PrintWriter Example:

The PrintWriter class is used to write formatted text to a file. It is a character-oriented class which is used to write data in characters.


import java.io.PrintWriter;
import java.io.FileNotFoundException;

class PrintWriterExample {
    public static void main(String[] args) {
        try {
            PrintWriter writer = new PrintWriter("printWriterOutput.txt");
            writer.println("Hello, PrintWriter!");
            writer.close();
            System.out.println("Successfully wrote to the file using PrintWriter.");
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using PrintWriter.

Using FileOutputStream

FileOutputStream Example:

The FileOutputStream class is used to write byte-oriented data to a file. It is mainly used for writing streams of raw bytes.


import java.io.FileOutputStream;
import java.io.IOException;

class FileOutputStreamExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("fileOutputStream.txt");
            String str = "FileOutputStream example.";
            byte[] strToBytes = str.getBytes();
            fos.write(strToBytes);
            fos.close();
            System.out.println("Successfully wrote to the file using FileOutputStream.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using FileOutputStream.

Using Files.write()

Files.write() Example:

The Files.write() method is a convenient way to write data to a file. It is part of the java.nio.file package and provides an efficient way to write data.


import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;

class FilesWriteExample {
    public static void main(String[] args) {
        String content = "This is an example using Files.write().";
        try {
            Files.write(Paths.get("filesWrite.txt"), content.getBytes());
            System.out.println("Successfully wrote to the file using Files.write().");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using Files.write().

Using FileChannel

FileChannel Example:

The FileChannel class is part of the NIO package and provides an efficient way to read from and write to files. It allows for non-blocking I/O operations.


import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.io.IOException;
import java.io.RandomAccessFile;

class FileChannelExample {
    public static void main(String[] args) {
        String data = "This is an example using FileChannel.";
        try (RandomAccessFile stream = new RandomAccessFile("fileChannel.txt", "rw");
             FileChannel channel = stream.getChannel()) {
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put(data.getBytes());
            buffer.flip();
            channel.write(buffer);
            System.out.println("Successfully wrote to the file using FileChannel.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using FileChannel.

Using DataOutputStream

DataOutputStream Example:

The DataOutputStream class allows an application to write primitive Java data types to an output stream in a portable way.


import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

class DataOutputStreamExample {
    public static void main(String[] args) {
        try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("dataOutputStream.txt"))) {
            dos.writeUTF("Writing with DataOutputStream.");
            System.out.println("Successfully wrote to the file using DataOutputStream.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using DataOutputStream.

Using RandomAccessFile

RandomAccessFile Example:

The RandomAccessFile class in Java is used to read and write to a random access file. It allows you to move the file pointer to read or write at any position in the file.


import java.io.IOException;
import java.io.RandomAccessFile;

class RandomAccessFileExample {
    public static void main(String[] args) {
        try (RandomAccessFile raf = new RandomAccessFile("randomAccessFile.txt", "rw")) {
            raf.writeUTF("Writing with RandomAccessFile.");
            System.out.println("Successfully wrote to the file using RandomAccessFile.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using RandomAccessFile.

Using OutputStreamWriter

OutputStreamWriter Example:

The OutputStreamWriter class is a bridge from byte streams to character streams. It writes bytes to the output stream as characters using a specified charset.


import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.io.IOException;

class OutputStreamWriterExample {
    public static void main(String[] args) {
        try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("outputStreamWriter.txt"))) {
            osw.write("Writing with OutputStreamWriter.");
            System.out.println("Successfully wrote to the file using OutputStreamWriter.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully wrote to the file using OutputStreamWriter.

Using FileWriter with Append Mode

FileWriter with Append Mode Example:

The FileWriter class can be used in append mode by passing true as the second parameter in its constructor. This allows new data to be appended to the end of the file.


import java.io.FileWriter;
import java.io.IOException;

class FileWriterAppendExample {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("appendMode.txt", true);
            writer.write("Appending data to the file.\n");
            writer.close();
            System.out.println("Successfully appended to the file using FileWriter.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
    

Console Output:

Successfully appended to the file using FileWriter.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025