我尝试使用绝对路径从文件系统读取文件,但由于"FileNotFoundException“而失败,我不知道为什么
File file=new File("E:\\Directory\\File.txt");
byte[] buff=new byte[8];
FileInputStream fileIn=new FileInputStream(file.getAbsolutePath());
int n=fileIn.read(buff);
System.out.println(n);发布于 2016-11-05 19:53:11
以下是可能导致此问题的一些因素:
- It might have invisible / non-printing characters in it.
- It might have trailing space characters, or different numbers of embedded spaces
- It might be a homoglyph problem
我还可以建议你这样试一下吗:
File file = new File("E:/Directory/File.txt");
FileInputStream fileIn = new FileInputStream(file);Java路径名处理应将"/“转换为适当的特定于平台的文件分隔符。而且file已经表示了一个绝对路径,所以调用file.getAbsolutePath()应该是不必要的。
发布于 2016-11-05 22:50:26
谢谢你的帮助。我已经知道它可能会抛出异常,所以我应该在方法签名中添加异常,或者用why.Because /catch包围它。实际上,当你使用Intelli时,“Alt+Enter”可以帮助你找出你的代码出了什么问题.
发布于 2016-11-05 19:31:28
好吧,问题就像错误所说的那样:
该文件不存在。即在该路径中没有文件。您为构造函数指定的名称可能是错误的。您可以通过以下方式检查文件是否存在:
File file=new File("E:\\Directory\\File.txt");
if(file.exists()){
//do things here
}你也可以从windows资源管理器检查文件是否存在(假设你在你的pc上运行windows )。如果你找不到这个文件,那就解释了这个问题。正如我所说的,抛出错误是因为没有具有该路径的文件。
https://stackoverflow.com/questions/40437705
复制相似问题