Currently I am using scanner/filereader and using while hasnextline. I think this method is not highly efficient. Is there any other method to read file with the similar functionality of this?
public void Read(String file) { Scanner sc = null; try { sc = new Scanner(new FileReader(file)); while (sc.hasNextLine()) { String text = sc.nextLine(); String[] file_Array = text.split(" ", 3); if (file_Array[0].equalsIgnoreCase("case")) { //do something } else if (file_Array[0].equalsIgnoreCase("object")) { //do something } else if (file_Array[0].equalsIgnoreCase("classes")) { //do something } else if (file_Array[0].equalsIgnoreCase("function")) { //do something } else if (file_Array[0].equalsIgnoreCase("ignore")) { //do something } else if (file_Array[0].equalsIgnoreCase("display")) { //do something } } } catch (FileNotFoundException e) { System.out.println("Input file " + file + " not found"); System.exit(1); } finally { sc.close(); } }
Advertisement
Answer
You will find that BufferedReader.readLine()
is as fast as you need: you can read millions of lines a second with it. It is more probable that your string splitting and handling is causing whatever performance problems you are encountering.