FileInputStream不适用于相对路径 [英] FileInputStream doesn't work with the relative path
本文介绍了FileInputStream不适用于相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试从 FileInputStream
创建一个对象,并将文件的相对值传递给它的构造函数,但它无法正常工作并抛出 FileNotFoundException
I tried to create an object from FileInputStream
and pass the relative value of a file to its constructor, but it doesn't work properly and threw a FileNotFoundException
try {
InputStream is = new FileInputStream("/files/somefile.txt");
} catch (FileNotFoundException ex) {
System.out.println("File not found !");
}
推荐答案
/
在开始时将使路径成为绝对路径而不是相对路径。
The /
at the start will make the path absolute instead of relative.
尝试删除前导 /
,所以替换:
Try removing the leading /
, so replace:
InputStream is = new FileInputStream("/files/somefile.txt");
with:
InputStream is = new FileInputStream("files/somefile.txt");
如果您仍遇到问题,请尝试确保程序从您认为的位置开始运行< a href =https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java>检查当前目录:
If you're still having trouble, try making sure the program is running from where you think by checking the current directory:
System.out.println(System.getProperty("user.dir"));
这篇关于FileInputStream不适用于相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文