In: Computer Science
/*If you any query do comment in the comment section else like the solution*/
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class ReadFileLineByLine {
public static void main(String[]args) throws FileNotFoundException {
File file = new File("Pass Your File name with complete path");
Scanner sc = new Scanner(file);
int count = 0, maxLength = 0, maxIndex = 0;
while(sc.hasNextLine()) {
String tmp = sc.nextLine();
if(maxLength < tmp.length()) {
maxLength = tmp.length();
maxIndex = count + 1;
}
count++;
}
sc.close();
System.out.print("The file has " + count + " lines. ");
System.out.print("The longest line is line " + maxIndex + " and it has " + maxLength + " characters.");
}
}