我对java相当陌生,我正在尝试查找LINUX路径中指定的文件是否存在。
private void validateFilePath(String filePath) {
File dir = new File(filePath);
if(dir.exists()){
System.out.println("File exists in the path " + dir);
setTARGET_IMG_DIR("filePath");
return;
}else{
System.out.println("File does not exists in the path: " + dir);
return;
}
}
如果我像这样从根目录提供绝对路径,则 dir.exists 可以正常工作
/Users/yv/Documents/Eclipse-workspace/InputParser/bin/test.txt
但是如果我给出一个相对路径,
test.txt
或者/InputParser/bin/test.txt
它说文件不存在。
我打算创建一个这个项目的 jar,因此这应该适用于相对路径(同一目录中的文件)和来自根目录的绝对路径。我该如何处理?
是否可以从根目录搜索该文件的绝对路径并将其附加到文件名?