系统找不到指定的路径 - 文本文件

The system cannot find the path specified - text file

我在我的项目中添加了一个文本文件,如下所示:

Myproject/WebPages/stopwords.txt

图片: http://s7.postimg.org/w65vc3lx7/Untitled.png

我试图打开文件,但打不开!

我的代码:

BufferedReader br = new BufferedReader(new FileReader("stopwords.txt"));
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

错误: stopwords.txt(系统找不到指定的路径)

FileReader 将打开相对于执行路径的文件。如果这是从 "MyProject" 文件夹执行的,您将需要在 FileReader 构造函数中指定文件夹,如 FileReader("WebPages/stopWords.txt")

如果你的项目路径是根文件夹。 class 所在的位置不是根文件夹所在的位置。将其更改为此应该有效。您还需要在文件名中添加 space。

BufferedReader br = new BufferedReader(new FileReader("/Web Pages/stopwords.txt"));

你可以试试,

我猜你在 .jsp 文件中。

<%

String path = request.getServletContext().getRealPath("/WebContent/stopwords.txt") ;
BufferedReader br = new BufferedReader(new FileReader(path));
// other codes...
 %>

已编辑:

<% 
  String path = request.getServletContext().getRealPath("/stopwords.txt") ;
  //check here with print path variable...    
  // you can pass this path variable to invoke method which is reside into //your java class...
  BufferedReader br = new BufferedReader(new FileReader(path));


%>

经验法:

开始创建和写入一些文件 => 然后您将看到它在文件系统中的位置

然后把你的文件放在同一个目录下,再尝试其他方法读取它

警告:有时,您无法在某些目录中读取或写入。