【发布时间】:2018-11-08 01:28:20
【问题描述】:
我这里有问题。每当我输入从我的计算机复制的目录路径以将一个txt文件输入到我的程序中时,它总是说找不到该文件。我的代码有问题吗?
System.out.println("insert directory file = ");
FileReader file = null;
try {
file = new FileReader(input.next());
BufferedReader readfile = new BufferedReader(file);
StringBuffer sb = new StringBuffer();
try {
while ((text = readfile.readLine()) != null) {
sb.append(text);
sb.append("\n");
}
readfile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text = sb.toString();
//System.out.println(text);
System.out.println("Data entered");
System.out.println("Data length = "+text.length()+"\n");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
System.out.println("File not found. Pease insert the proper file directory.\n");
}
【问题讨论】:
-
您能否通过将 e1.printStackTrace() 添加到您的捕获中来包含异常消息输出。这将包含有关无法找到的文件名的信息。
-
您也应该提供粘贴到控制台的路径。我猜有一个复制/粘贴错误
-
我这样写 D:\\Home\\data.txt 但这不起作用,我的代码是否已经正确?
-
我已经尝试过 e1.printStackTrace 但在我进入路径目录后这让我的程序停止
标签: java filereader