io 遍历文本、查询指定内容

File file = new File("E:\test\hello.txt");

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));


        List<String> list = new ArrayList<>();

        String lineTxt = null;
        /*while ((lineTxt = br.readLine()) != null){

            if (lineTxt.contains("10014")){

                list.add(lineTxt);
            }
        }*/


            while ((lineTxt = br.readLine()) != null){
          
   

                if (lineTxt.startsWith("m",5)){
          
   
                        list.add(lineTxt);
                }

                if (list.size() == 5){
          
   
                    break;
                }

            }




        for (String s : list) {
          
   
            System.out.println(s);
        }

        if (br != null){
          
   
            try {
          
   
                br.close();
            } catch (IOException e) {
          
   
                e.printStackTrace();
            }
        }

    }
}
经验分享 程序员 微信小程序 职场和发展