package com.journaldev.readfileslinebyline;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFileLineByLineUsingBufferedReader {
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(
"/Users/pankaj/Downloads/myfile.txt"));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
// read next line
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Scanner sc = null;
try {
File file = new File("myfile.txt"); // java.io.File
sc = new Scanner(file); // java.util.Scanner
String line;
while (sc.hasNextLine()) {
line = sc.nextLine();
// process the line
}
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
finally {
if (sc != null) sc.close();
}