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.
Basic File Operations Tutorials
- Java – Create a file Java Tutorial to create a file at specified path using Files.write() method.
- Java – Read file as string Java Tutorial to read file as a string using BufferedInputStream, or FileUtils from commons.io.
- Java – Read file as bytes
- 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 – Get file size
- 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.
File Properties
- Java – Get created timestamp of file
- Java – Get last modified timestamp of file
- Java – Update last modified date of file
- Java – Get extension of file
Checks
- Java – Check if file exists
- 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
- Java – Create directory
- Java – Check if directory exists
- Java – Delete directory
- Java – Rename directory
Others
- Java – Read contents of a file line by line using BufferedReader Java Tutorial to read the contents of a text file, line by line, using BufferedReader class.
- Java – Read contents of a File line by line using Stream Java Tutorial to read the contents of a text file, line by line, using Streams.