开发者

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

开发者 https://www.devze.com 2022-12-21 16:26 出处:网络
I was wondering, If i had a java class, that wanted to consult a txt file with say a list of names like

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 co开发者_JAVA技巧ntained 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..


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号