Java Topic
Java File Handling
File handling in Java involves reading from and writing to files. Java provides various classes and methods for file manipulation, primarily located in the java.io and java.nio.file packages.
1. File Handling with java.io Package
The java.io package provides classes for handling files. Some commonly used classes include File, FileReader, FileWriter, BufferedReader, and BufferedWriter.
Creating a File
import java.io.File;
public class CreateFile {
public static void main(String[] args) {
try {
File file = new File("example.txt");
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
This example demonstrates how to create a new file. If the file already exists, a message is displayed.
Writing to a File
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFile {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) {
writer.write("Hello, World!");
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
This example demonstrates how to write a string to a file using BufferedWriter.
Reading from a File
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFromFile {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
This example demonstrates how to read from a file line by line using BufferedReader.
2. File Handling with java.nio.file Package
The java.nio.file package provides the Files class, which offers static methods for file operations. This package is part of the New I/O (NIO) library introduced in Java 7.
Writing to a File
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;
public class NIOWriteExample {
public static void main(String[] args) {
List<String> lines = List.of("Hello, World!", "Welcome to Java NIO.");
try {
Files.write(Paths.get("example.txt"), lines);
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
This example demonstrates how to write multiple lines to a file using the Files.write method.
Reading from a File
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;
public class NIOReadExample {
public static void main(String[] args) {
try {
List<String> lines = Files.readAllLines(Paths.get("example.txt"));
lines.forEach(System.out::println);
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
This example demonstrates how to read all lines from a file and print them using the Files.readAllLines method.
Comments (0)
No comments yet. Be the first to comment!