Oct 10, 2018

[HDGEM] How to read line with Scanner in Java

This code reads the file line by line.
public static void readFileByLine(String fileName) {
  try {
   File file = new File(fileName);
   Scanner scanner = new Scanner(file);
   while (scanner.hasNext()) {
    System.out.println(scanner.next());
   }
   scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } 
 }
You can also set a delimiter as a line separator and then perform the same.
 scanner.useDelimiter(System.getProperty("line.separator"));


--
Posted By Blogger to HDGEM at 2/11/2017 08:29:00 AM