【问题标题】:Find word in file - if file contains will output path to console [closed]在文件中查找单词 - 如果文件包含将输出到控制台的路径 [关闭]
【发布时间】:2013-01-22 22:11:08
【问题描述】:

我需要创建一个程序,它会做一些事情:

  • 带参数:要查找的单词和要查找的路径
  • 在文件中一一查找给定的单词
  • 如果在文件中找到该单词 -> 打印到控制台filename - file path

用相同的解析算法遍历所有文件夹,直到有可能。

这里是代码片段:

class SearchPhrase {
    // walk to root way
    public void walk(String path, String whatFind) throws IOException {
        File root = new File(path);
        File[] list = root.listFiles();
        for (File titleName : list) {
            if (titleName.isDirectory()) {
                walk(titleName.getAbsolutePath(), whatFind);
            } else {
                if (read(titleName.getName()).contains(whatFind)) {
                    System.out.println("File: " + titleName.getAbsoluteFile());
                }
            }
        }
    }

    // Read file as one line
    public static String read(String fileName) {
        StringBuilder strBuider = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsoluteFile()));
            String strInput;
            while ((strInput = in.readLine()) != null) {
                strBuider.append(strInput);
                strBuider.append("\n");
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return strBuider.toString();
    }

    public static void main(String[] args) {

        SearchPhrase example = new SearchPhrase();
        try {
            example.walk("C:\\Documents and Settings\\User\\Java", "programm");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

程序无法编译并出现以下错误:

java.io.FileNotFoundException: C:\Documents and Settings\User\Java Hangman\Java\Anton\org.eclipse.jdt.core.prefs at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileReader.<init>(FileReader.java:72) at task.SearchPhrase.read(SearchPhrase.java:28) at task.SearchPhrase.walk(SearchPhrase.java:16) at task.SearchPhrase.walk(SearchPhrase.java:14) at task.SearchPhrase.main(SearchPhrase.java:48)

也许是解决此问题的另一种方法?

【问题讨论】:

  • 在这里帮助我们一点。您预计会发生什么,发生了什么?
  • 请准确详细地解释问题所在,而不是仅仅说“我犯了一些错误”。程序是否编译?如果没有,你会得到什么错误?它运行吗?你有例外吗?如果是,则显示完整的堆栈跟踪。

标签: java file search


【解决方案1】:

你在这里犯了几个错误..

[...]
  if (read(titleName.getName()).contains(whatFind)) {
                System.out.println("File: " + titleName.getAbsoluteFile());
      }
 [...]

在上面的代码中,您将文件名传递给错误的读取方法。相反,您必须像这样传递文件名及其路径...

    if (read(**titleName.getAbsolutePath()**).contains(whatFind)) {
                System.out.println("File: " + titleName.getAbsoluteFile());
     }

这里不需要getAbsoluteFile()

   [...]

       BufferedReader in = new BufferedReader(new FileReader(new File(
                fileName).**getAbsoluteFile()**));
   [...]

【讨论】:

  • sampath,非常感谢!现在一切正常!
  • System.out.println("File: " + titleName.getAbsoluteFile()) =&gt; + titleName.getAbsolutePath()
猜你喜欢
  • 2015-09-10
  • 2020-06-25
  • 2012-12-16
  • 2022-07-15
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多