Java File Operations

Using Java, you can do file operations like read a file, write to a file, append data to an existing file, delete a file, etc.

In this tutorial, we shall go through the basic CRUD Operations on Files and provide a list of general file operations using Java.

Java Basic File Operations

ADVERTISEMENT

Java – Create a file

Java Tutorial to create a file at specified path using Files.write() method.

File Read Operations in Java

Java – Read file as string

Java tutorial to read file as a string using BufferedInputStream, or FileUtils from commons.io.

Java – Read file line by line using BufferedReader

Java tutorial to read the contents of a text file, line by line, using BufferedReader class.

Java – Read file line by line using Stream

Java tutorial to read the contents of a text file, line by line, using Streams.

Java – Read file as byte array

Java tutorial to read a file as bytes in Java, using readAllBytes() method of java.nio.file.Files class.

Path filePath = Paths.get("/path/to/file");
byte[] fileBytes = Files.readAllBytes(filePath);

Java – Read file as inputstream

Java tutorial to read a file as InputStream using java.io.FileInputStream class.

InputStream inputStream = new FileInputStream("/path/to/file");

Java – Read file as JSON

Java tutorial to write string to a file using BufferedOutputStream, or FileUtils.writeStringToFile() of commons.io.

Java – Read file as string from resources

Java tutorial to read file as a string from resources using ClassLoader, InputStream, and Scanner classes.

InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("path/to/file/in/resources");
Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
String fileContent = scanner.next();

Java – Read file as lines

Java tutorial to write string to a file using BufferedOutputStream, or FileUtils.writeStringToFile() of commons.io.

Java – Read file as list of strings

Java tutorial to read a file as List of strings using java.nio.file.Files.readAllLines() method.

Path path = Paths.get("/path/to/file");
List<String> lines = Files.readAllLines(path);

File Write Operations in Java

Java – Write string to file

Java tutorial to write string to a file using BufferedOutputStream, or FileUtils.writeStringToFile() of commons.io.

Java – Copy File

Java Tutorial to copy a file using Input and Output streams, Files.copy() function, or FileUtils.copyFile() method.

Java – Delete File

Java Tutorial to delete a file using File.delete() function.

Java – Rename File

Java Tutorial to rename a file using File.renameTo() function.

Java – Download File from URL

Java Tutorial to download a file using FileUtils.copyURLToFile() function of commons.io.

Java – Replace a String in File

Java Tutorial to read a file to a string, then replace a string with replacement using String.replace(), then writing the resulting string back to file.

Java – Filter list of files or directories in a folder

Java Tutorial to get the list of files and sub-folders in a folder, filter them by extension or file type.

Getting File Meta Information in Java

Java – Get file size

Java tutorial to get the size of file, i.e., the number of bytes in the file, using java.nio.file.Files size() method.

Path filePath = Paths.get("/path/to/file");
long fileSize = Files.size(filePath);

Java – Get file creation time

Java tutorial to get the file creation time of a file, using creationTime() method of java.nio.file.attribute.BasicFileAttributes class.

Path filePath = Paths.get("/path/to/file");
BasicFileAttributes attributes = Files.readAttributes(filePath, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();

Java – Get file last modified time

Java tutorial to get the file last modified time of a file, using creationTime() method of java.nio.file.attribute.BasicFileAttributes class.

Path filePath = Paths.get("/path/to/file");
BasicFileAttributes attributes = Files.readAttributes(filePath, BasicFileAttributes.class);
FileTime lastModifiedTime = attributes.lastModifiedTime();

Checking on Files in Java

Java – Check if file exists

Java tutorial to check if file exists using File.exists() method. The method returns true if the file exists, or else, it returns false.

Java – Check if file is readable

Java tutorial to check if file is readable using File.canRead() function.

Java – Check if file is writable

Java tutorial to check if file is writable using File.canWrite() function.

Java – Check if file is executable

Java tutorial to check if file is executable using File.canExecute() function.

Directory / Folder Operations in Java

Java – Create directory

Java – Create directory recursively

Java – Check if directory exists

Java – Delete directory

Java – Rename directory