Java NIO Create, Write, Read and Delete Files

In this tutorial, we will learn how to create, write, read and remove files using Java NIO package APIs.

Java Create File Example 

We use Files.createFile() method to create a file in Java.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.HashSet;
import java.util.Set;

public class JavaCreateFile {

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

        Set<PosixFilePermission> perms = new HashSet<>();

        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_WRITE);
        perms.add(PosixFilePermission.OTHERS_READ);

        FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);

        Path myPath = Paths.get("src/resources/sample.txt");

        if (Files.exists(myPath)) {
            
            System.out.println("File already exists");
        } else {
            
            Files.createFile(myPath, attrs);
            System.out.println("File created");
        }
    }
}
Create a new file named "sample.txt" if the file does not exist:
        if (Files.exists(myPath)) {
            
            System.out.println("File already exists");
        } else {
            
            Files.createFile(myPath, attrs);
            System.out.println("File created");
        }

Java Write File Example

We use Files.write() method to write a text or string to the given file.

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 Read File Example

We use Files.readAllLines() method to read all lines from a given file.
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 Delete File Example

We 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");
        }
    }
}

Output:

File deleted

References


Comments