java - 找不到指定的文件?

标签 java file java-io

我是不是疯了?我的 BufferedReader 找不到指定的文件,但我绝对确定我有正确的路径(我已经检查了无数次,单步执行...我不知道发生了什么)。

这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


/**
* Code for E11.8. Searches all files specified on the command line and 
prints out all lines     
containing a specified word.
* @author - Matthew Arnold
*/
public class Find {
/**
  Searches file for a word, prints out all lines containing that word.
  @param wordToFind the word to find
  @param filename the filename for the file to search
*/
public static void findAndPrint(String wordToFind, String fileName)throws IOException{

   BufferedReader br = new BufferedReader(new FileReader(fileName));

   ArrayList<String> stringsFromFile = new ArrayList<>();

   while(br.ready()) {
       stringsFromFile.add(br.readLine());
   }

   for(String s : stringsFromFile) {
       if(s.contains(wordToFind))
           System.out.println(s);
   }

   br.close();
}

 /**
  First argument of the main method should be the word to be searched
  For other arguments of the main method, store the file names to be examined
*/
public static void main(String[] args)
{
   String fileName = System.getProperty("user.home") + "\\Desktop\\mary.txt";
  System.out.println(fileName);


  try {
        findAndPrint("lamb", fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 }
}

也许另一双眼睛可以看到我所看不到的东西。任何帮助是极大的赞赏。谢谢。

编辑 - 添加到推荐的测试行并包含堆栈跟踪:

更新的代码(主要):

 public static void main(String[] args)
 {
   String fileName = System.getProperty("user.home") + 
 "\\Desktop\\mary.txt";
  System.out.println(fileName);


  try {
        System.out.println(new File(fileName).getAbsolutePath());
        findAndPrint("lamb", fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

 }
}

输出:

C:\Users\Matthew\Desktop\mary.txt
C:\Users\Matthew\Desktop\mary.txt
java.io.FileNotFoundException: C:\Users\Matthew\Desktop\mary.txt (The 
system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Find.findAndPrint(Find.java:21)
at Find.main(Find.java:50)

最佳答案

我认为问题可能出在路径分隔符上,您可以使用 File.separator 并执行类似的操作:

  public static void main(String[] args)
  {
     String fileName = String.join(File.separator,System.getProperty("user.home"),"Desktop","mary.txt");
    System.out.println(fileName);

    try {
        findAndPrint("lamb", fileName);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

关于java - 找不到指定的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52485704/

相关文章:

windows - Windows下如何查找Tomcat安装盘符

java - Spring:将@RequestBody 注入(inject)@Bean

java - 从文件中读取单词后,按字母顺序在 java 中创建单词数组

c - fopen() 在 SD 卡上写入时返回无效参数?

java - 无法抗拒工作

Java - 无法通过 url 读取图像

java - 我是否在异步任务中实现警报对话框?

java - 为什么 Vector.indexOf 方法不适用于此类?

java - 如何在从 gradle jar 任务生成的 jar 中包含运行时依赖项

iOS 从 iCloud Drive 下载文件