There are number of ways to read a simple text file in java like BufferedReader, Scanner or FileReader.
1. Scanner Class -
1. Scanner Class -
- Scanner class reads the file as well as it can parse it too. It has methods like nextInt(), nextDouble() etc. So that it can directly read integer data from file unlike other file reading methods.
- It can read a file word-by-word, line-by-line , or complete file at once
import java.io.File; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { try { // Use scanner to read input file Scanner scan = new Scanner(new File("testFile.txt")); // Iterate while loop till the last line in file // This loop read a text file line-by-line and prints it while (scan.hasNext()) { // Print the line System.out.println(scan.nextLine()); } scan.close(); } catch (Exception e) { e.printStackTrace(); } } }
Read file word-by-word

Read full file at once

2. BufferedReader Class-

3. FileReader Class-

NOTE - You can refer this article to learn how to read a file line-by-line , split it and store it into different variable. Link - Java read data from file into array of objects
import java.io.File; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { try { // Use scanner to read input file Scanner scan = new Scanner(new File("testFile.txt")); // Iterate while loop till the last line in file // This loop reads a text file word-by-word and prints it while (scan.hasNext()) { // Print the word System.out.println(scan.next()); } scan.close(); } catch (Exception e) { e.printStackTrace(); } } }Output-
Read full file at once
import java.io.File; import java.util.Scanner; public class ReadFile { public static void main(String[] args) { try { // Use scanner to read input file Scanner scan = new Scanner(new File("testFile.txt")); // We would have to use delimiter \\Z // which means end of string scan.useDelimiter("\\Z"); // Print the line System.out.println(scan.next()); scan.close(); } catch (Exception e) { e.printStackTrace(); } } }Output-
2. BufferedReader Class-
- BufferedReader class are always wrapped around FileReader or InputStreamReaders
import java.io.BufferedReader; import java.io.FileReader; public class ReadFile { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("testFile.txt")); String line = ""; // This loop will be iterated till the end of the file // Each line will be read and assigned to "line" while((line=br.readLine())!=null) { // Print line System.out.println(line); } br.close(); } catch (Exception e) { e.printStackTrace(); } } }Output -
3. FileReader Class-
- These are mainly used to read character files
import java.io.FileReader; public class ReadFile { public static void main(String[] args) { try { FileReader fr = new FileReader("testFile.txt"); int i; // This loop will be iterated till the end of the file // Here i is the ASCII value of the respective character in file while((i=fr.read())!=-1) { // Print ASCII value and character System.out.println(i+" "+(char)i); } fr.close(); } catch (Exception e) { e.printStackTrace(); } } }Output-
NOTE - You can refer this article to learn how to read a file line-by-line , split it and store it into different variable. Link - Java read data from file into array of objects
Nice content.
ReplyDeleteThank you !!
Delete