java - 找不到指定的文件?
问题描述
我失去理智了吗?我的 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();
}
}
推荐阅读
- bash - 如果条件不考虑第二个条件,为什么我的 bash?
- scala - 如何使用 Scala 的异常处理从 JDBC 连接类返回连接?
- php - 在 Laravel6.x 中获取文件(图像)尺寸
- react-native - 我应该如何使用 React Native 实现像 spotify 这样的迷你播放器?
- cakephp-2.0 - cakephp 2.x 中的 Flash/Session 消息
- python - 无法将具有关系的类似类型的数据(在这种情况下是附近的朋友)从朋友姓名的大数据中分组到一个文件中
- r - 我在使用fmincon时出现错误,不知道我是否正确使用它?
- spring-security - 没有找到类 org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken 的提供者
- android - 如何使用 FirebaseUI Firestore RecyclerView 显示图像
- flutter - 如何读取二维码并将其裁剪为 Flutter 中的新图像