February 11, 2017

[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
Post a Comment

Featured Post

The new Dell XPS 13’s overall design is 30% thinner and lighter than its predecessor

The new Dell XPS 13 has twice the performance of the XPS 13 launched in 2015, as well as improved battery life that should enable the model...