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

Payara Server – Robust, Reliable, Supported

Payara Server is an open source application server derived from GlassFish, with an option of 24/7 Production and Migration & Project Sup...