Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

File Handling in java → Read file

File Handling in java

Read file

Reading Files in Java

Java provides multiple ways to read data from text files. The choice of method depends on factors like file size, desired level of efficiency, and the type of data manipulation required. Here are three common methods:

1. Using FileReader and BufferedReader:

FileReader: This class creates a stream for reading characters from a text file. BufferedReader: This class wraps around a FileReader and provides buffering capabilities for improved performance, especially when reading larger files.
Reading file in java try { // Create a FileReader object for the file path FileReader reader = new FileReader("data.txt"); // Create a BufferedReader for efficiency BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); // Process each line } bufferedReader.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); // Handle potential exceptions }
Explanation: FileReader reader = new FileReader("data.txt");: This line creates a FileReader object associated with the file path "data.txt". BufferedReader bufferedReader = new BufferedReader(reader);: This line creates a BufferedReader object that wraps around the FileReader for buffering. while ((line = bufferedReader.readLine()) != null) { ... }: This loop iterates line by line until readLine() returns null (indicating the end of the file). Inside the loop, you can process each line (line) as needed. bufferedReader.close() and reader.close(): These lines close the reader objects, releasing resources and ensuring all data is read from the underlying file.

2. Using Scanner Class:

Scanner: This class offers a more versatile approach for reading both primitive data types (like int, double) and formatted strings from text files. It can also handle different delimiters (e.g., comma, space) to separate tokens within a line.
Reading file using scanner try { Scanner scanner = new Scanner(new File("data.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); // Process the line using scanner methods like nextInt(), nextDouble() etc. } scanner.close(); } catch (IOException e) { e.printStackTrace(); // Handle potential exceptions }
Explanation: Scanner scanner = new Scanner(new File("data.txt"));: This line creates a Scanner object linked to the File object representing "data.txt". while (scanner.hasNextLine()) { ... }: This loop iterates as long as the scanner has more lines to read. String line = scanner.nextLine();: This line reads the next line of text from the file and stores it in the line variable. Inside the loop, you can use scanner methods like nextInt(), nextDouble(), etc., to parse data based on your file format. scanner.close(): This line closes the scanner object, releasing resources.

3. Using Files.readAllLines() (Java 7 and above):

Files Class (Java 7+): This class provides static methods for various file operations. Here, we'll use readAllLines(). readAllLines(): This method reads all lines from a text file into a List<String>. It's suitable for smaller files where you need to process the entire content at once.
Reading files using readAllLines() method try { List<String> lines = Files.readAllLines(Paths.get("data.txt")); for (String line : lines) { System.out.println(line); // Process each line } } catch (IOException e) { e.printStackTrace(); // Handle potential exceptions }
Explanation: List<String> lines = Files.readAllLines(Paths.get("data.txt"));: This line uses Files.readAllLines() to read all lines from the file and store them in a List of strings. Paths.get("data.txt") creates a path object for the file. for (String line : lines) { ... }: This loop iterates over each line in the lines list.