1. Using Scanner:
The Scanner class is suitable for reading formatted data, line by line, or token by token.
It can be used with a File object to read from a file.
You can use methods like nextLine(), nextInt(), next(), etc., to read different types of data.
You can use hasNextLine() to check if there are more lines to read.
Java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
File file = new File("your_file.txt"); // Replace with your file path
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.contains("search_term")) { // Replace with your search term
System.out.println("Found: " + line);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
2. Using BufferedReader:
BufferedReader is efficient for reading text files line by line.
It provides buffering for faster reading.
You can use the readLine() method to read a line at a time and check for null to identify the end of the file.
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) {
String filePath = "your_file.txt"; // Replace with your file path
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("search_term")) { // Replace with your search term
System.out.println("Found: " + line);
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
3. Using Files (NIO):
The Files class from the java.nio.file package offers more modern ways to handle files.
Files.readAllLines() reads all lines of a file into a List<String>.
Files.lines() returns a Stream<String>, which can be used for more advanced processing.
Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;
public class ReadFile {
public static void main(String[] args) {
Path filePath = Paths.get("your_file.txt"); // Replace with your file path
try {
// Using readAllLines
List<String> lines = Files.readAllLines(filePath);
for (String line : lines) {
if (line.contains("search_term")) { // Replace with your search term
System.out.println("Found: " + line);
}
}
// Using lines stream
try (Stream<String> stream = Files.lines(filePath)) {
stream.filter(line -> line.contains("search_term")) // Replace with your search term
.forEach(line -> System.out.println("Found: " + line));
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
Key points:
File Path: Replace "your_file.txt" with the actual path to your file.
Search Term: Replace "search_term" with the word or phrase you want to find.
Error Handling: Use try-catch blocks to handle potential FileNotFoundException or IOException
File Existence: You can use File.exists() before attempting to read to check if the file exists.
Resource Management: Use try-with-resources to ensure that resources like Scanner, BufferedReader, and streams are closed automatically.
Choosing the Right Method:
Scanner is good for formatted input or when you need to read tokens/words.
BufferedReader is efficient for line-by-line reading.
Files (NIO) is more modern and flexible, especially for larger files or when you want to use streams.
No comments:
Post a Comment