从Windows中的相对路径获取文件 [英] Get file from relative path in windows
本文介绍了从Windows中的相对路径获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的项目目录访问.doc文件.在Windows中使用相对路径时出现错误.但是当我获得绝对路径时,它就可以正常工作.
I am trying to access .doc a file from my project directory. I am getting an error when I use a relative path in windows. but it works fine when I get an absolute path.
File initialFile = new File("D:\\Demo\\src\\test\\java\\com\\pro\\mockfiles\\My-DOC-FILE.doc");
InputStream uploadStream = new FileInputStream(initialFile);
工作正常 但是,
File initialFile = new File("test/java/com/pro/mockfiles/My-DOC-FILE.doc");
InputStream uploadStream = new FileInputStream(initialFile);
出现以下错误
java.io.FileNotFoundException: test\java\com\pro\mockfiles\My-DOC-FILE.doc (The system cannot find the path specified)
我想使用相对路径,您能帮忙吗?
I want to run with a relative path, can you help?
推荐答案
此处是另一种解决方案,如何获取目录,该应用程序已在以下位置启动:
Here is an other solution, how to get the directory, the application was started in:
String current_dir = System.getProperty("user.dir");
此后,可以使用当前工作目录构建.doc
的绝对路径:
After this the absolute path to the .doc
can be built using the current working directory:
Paths.get(current_dir, "test/java/com/pro/mockfiles/My-DOC-FILE.doc");
这篇关于从Windows中的相对路径获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文