Java文件I/O-文本文件-如何检查内容? [英] Java File I/O - Text File - How to check for content?

查看:127
本文介绍了Java文件I/O-文本文件-如何检查内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果我有一个Java类,想查询一个txt文件,并说出类似的名字

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

我如何才能在Java程序中打开文本文件,并基本上查看程序中包含的字符串是否与这些名称之一匹配?

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();
            }

但不要无路可走..

推荐答案

您需要存储readLine()的结果,例如:

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

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

(当然,其中stringToCheck是目标字符串.)

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

这篇关于Java文件I/O-文本文件-如何检查内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆