File I/O operations are essential for interacting with the file system in Java. Advanced Java provides classes like File, FileInputStream, and FileOutputStream to handle file-related tasks such as creating, reading, writing, and managing files. This article explains how to perform advanced file I/O operations step by step with examples.
The File class represents file and directory pathnames and is used to perform file management tasks like creating, deleting, and checking file properties.
Example:
import java.io.File; public class FileExample { public static void main(String[] args) { try { // Create a File object File file = new File("example.txt"); // Create a new file if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } // Check file properties System.out.println("File path: " + file.getAbsolutePath()); System.out.println("Is writable: " + file.canWrite()); System.out.println("Is readable: " + file.canRead()); // Delete the file if (file.delete()) { System.out.println("File deleted successfully."); } } catch (Exception e) { e.printStackTrace(); } } }
The FileInputStream class is used to read binary data from a file. It reads data byte by byte or using a buffer.
Example:
import java.io.FileInputStream; public class FileInputStreamExample { public static void main(String[] args) { try { // Open the file input stream FileInputStream fis = new FileInputStream("input.txt"); // Read data byte by byte int byteData; while ((byteData = fis.read()) != -1) { System.out.print((char) byteData); // Convert byte to char } // Close the stream fis.close(); } catch (Exception e) { e.printStackTrace(); } } }
The FileOutputStream class is used to write binary data to a file. It allows overwriting or appending data.
Example:
import java.io.FileOutputStream; public class FileOutputStreamExample { public static void main(String[] args) { try { // Open the file output stream FileOutputStream fos = new FileOutputStream("output.txt"); // Write data to the file String data = "Advanced Java File I/O Example"; fos.write(data.getBytes()); // Convert string to bytes // Close the stream fos.close(); System.out.println("Data written to file successfully."); } catch (Exception e) { e.printStackTrace(); } } }
Combine FileInputStream and FileOutputStream to read from one file and write to another.
Example:
import java.io.FileInputStream; import java.io.FileOutputStream; public class FileCopyExample { public static void main(String[] args) { try { // Open input and output streams FileInputStream fis = new FileInputStream("source.txt"); FileOutputStream fos = new FileOutputStream("destination.txt"); // Read and write data in chunks byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } // Close streams fis.close(); fos.close(); System.out.println("File copied successfully."); } catch (Exception e) { e.printStackTrace(); } } }
Use try-with-resources to automatically close file streams and handle exceptions properly.
Example:
import java.io.FileInputStream; import java.io.FileOutputStream; public class TryWithResourcesExample { public static void main(String[] args) { try ( FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt") ) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } System.out.println("Data copied successfully."); } catch (Exception e) { e.printStackTrace(); } } }
Example:
import java.io.*; public class BufferedCopyExample { public static void main(String[] args) { try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("source.txt")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("destination.txt")) ) { byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = bis.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } System.out.println("Buffered file copy completed."); } catch (Exception e) { e.printStackTrace(); } } }
Advanced file I/O operations in Java using File, FileInputStream, and FileOutputStream provide a powerful way to manage and manipulate files. Understanding these operations is essential for building robust file-based applications.