Java File Create, Read, Write, Delete Example

In this tutorial, we will learn how to create, read, write, and delete a file in Java with examples.

Java 7 introduced java.nio.file.Files class that consists exclusively of static methods that operate on files, directories, or other types of files.

We gonna use java.nio.file.Files class methods to perform create, read, write, and delete file operations.

Java Create File Example

Let's use Files.createFile() method to create a file in Java.
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaCreateFileEx3 {

    public static void main(String[] args) throws IOException {

        Path path = Paths.get("src/main/resources/myfile.txt");

        try {
            Files.createFile(path);
            
        } catch (FileAlreadyExistsException ex) {
            
            System.err.println("File already exists");
        }
    }
}

Java Read File Example

Let's use Files.readAllLines() method to read all lines from a given file in Java.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class JavaReadFile {

    public static void main(String[] args) throws IOException {
        
        Path myPath = Paths.get("src/resources/sample.txt");
        
        List<String> lines = Files.readAllLines(myPath, StandardCharsets.UTF_8);
        
        lines.forEach(line -> System.out.println(line));
    }
}

Java Write File Example

Let's use Files.write() method to write a text or string to the given file in Java.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class JavaWriteFile {

    public static void main(String[] args) throws IOException {
        
        Path myPath = Paths.get("src/resources/sample.txt");
        
        List<String> lines = new ArrayList<>();
        lines.add("mango");
        lines.add("apple");
        lines.add("banana");
        lines.add("watermelon");
        lines.add("orange");
        
        Files.write(myPath, lines, StandardCharsets.UTF_8, 
                StandardOpenOption.CREATE);
        
        System.out.println("Data written to a file");
    }
}

Java Delete File Example

Let's use Files.deleteIfExists() method to delete a file if it exists.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaDeleteFile {

    public static void main(String[] args) throws IOException {
        
        Path myPath = Paths.get("src/resources/sample.txt");
        
        boolean fileDeleted = Files.deleteIfExists(myPath);
        
        if (fileDeleted) {
            
            System.out.println("File deleted");
        } else {
            
            System.out.println("File does not exist");
        }
    }
}

References

Comments