How to create a file in Java ?

There are many ways to create  a file in Java.

  1. Files.newBufferedWriter (Java 8)
  2. Files.write (Java 7)
  3. PrintWriter
  4. File.createNewFile

1)  Java 8 Files.newBufferedWriter

This example shows how to use the new Java 8 Files.newBufferedWriter to create and write to a file.

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateFileJava8 {
    public static void main(String[] args) throws Exception {
        String fileName = "C:\\training\\codehelp\\newFile.txt";

        Path path = Paths.get(fileName);

        // default, create, truncate and write to it.
        try (BufferedWriter writer =
                     Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {

            writer.write("Hello Coders !!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2) Java 7 Files.write

Below example shows how to use the Java 7 nio Files.write to create and write to a file. By default, it opens a file for writing; create the file if it doesn’t exist; truncate the current content if the file exists.

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

public class CreateFileNio {
    public static void main(String[] args) {
        String fileName = "C:\\training\\codehelp\\newFile.txt";
        String content = "hello coders!";

        try {

            // Java 1.7
            // default, create and write to it.
            Files.write(
                    Paths.get(fileName),
                    content.getBytes(StandardCharsets.UTF_8));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3) PrintWriter

Below  example shows how to use PrintWriter to create and write to a file.

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

public class CreateFile {
    public static void main(String[] args) {
        String fileName = "C:\\training\\codehelp\\newFile.txt";
        // Java 10, new constructor, support Charsets
        try (PrintWriter writer = new PrintWriter(fileName, StandardCharsets.UTF_8)) {

            writer.println("first line!");
            writer.println("second line!");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4) File.createNewFile()

Below example shows how to use the File.createNewFile() to create an empty file, and the FileWriter to write data to the file.

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

public class CreateFile2 {
    public static void main(String[] args) {
        String fileName = "C:\\training\\codehelp\\newFile1.txt";

        try {

            File file = new File(fileName);

            // true if file does no exist and was created successfully.
            // false if file is already exists
            if (file.createNewFile()) {
                System.out.println("File is created!");
            } else {
                System.out.println("File already exists.");
            }

            // Write to file
            try (FileWriter writer = new FileWriter(file)) {
                writer.write("Hello World!");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.