Java Write To File Examples

Amir
Amir
Java Write To File Examples

Java write to file examples

In this post we will look at five different examples on how to write to a file using Java. The code sinppets check to see if the file exists before writing to the file, otherwise a file is created.

Write to file using BufferedWriter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

Note: If we want to append to a file, we need to initialize the FileWriter with the true parameter:

1
FileWriter fw = new FileWriter(file, true);

Write to file using PrintWriter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            PrintWriter bw = new PrintWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

Write to file using FileOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream outStream = new FileOutputStream(file);
            byte[] strToBytes = content.getBytes();
            outStream.write(strToBytes);

            outStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

Write to file using Files class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WriteToFile {
    public static void main( String[] args ) {
        Path path = Paths.get("writefile.txt");
        String content = "Content to write to file";

        try {
            byte[] bytes = content.getBytes();
            Files.write(path, bytes);
        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

Write to file using DataOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        String content = "Content to write to file";

        try {
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            DataOutputStream dataOutStream = new DataOutputStream(bos);
            dataOutStream.writeUTF(content);
            dataOutStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}
See also