Read data from a text file using Java

后端 未结 16 1553
挽巷
挽巷 2020-12-10 05:35

I need to read a text file line by line using Java. I use available() method of FileInputStream to check and loop over the file. But while reading,

16条回答
  •  爱一瞬间的悲伤
    2020-12-10 06:03

    How about using Scanner? I think using Scanner is easier

         private static void readFile(String fileName) {
           try {
             File file = new File(fileName);
             Scanner scanner = new Scanner(file);
             while (scanner.hasNextLine()) {
               System.out.println(scanner.nextLine());
             }
             scanner.close();
           } catch (FileNotFoundException e) {
             e.printStackTrace();
           }
         }
    

    Read more about Java IO here

提交回复
热议问题