Java File I/O - Text File - How to check for content?

浪尽此生 提交于 2019-12-25 01:33:41

问题


I was wondering, If i had a java class, that wanted to consult a txt file with say a list of names like

tom
steve
jones

how could i open the text file in the java program and basically see if a string contained in the program matches one of these names?

so far i have come up with

                try {
                BufferedReader inputReader = new BufferedReader(new FileReader("users.txt"));

                while (inputReader.readLine() != null){

                }

            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException ep) {
                // TODO Auto-generated catch block
                p.printStackTrace();
            }

but do not no where to go from here..


回答1:


You need to store the result of readLine(), like:

String nextLine;
while ((nextLine = inputReader.readLine()) != null){
if (nextLine.equals(stringToCheck)) {
    //do something
  }
}

(where stringToCheck is the target string, of course.)



来源:https://stackoverflow.com/questions/2328735/java-file-i-o-text-file-how-to-check-for-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!