我想使用Java访问我的当前工作目录。
我的代码:
String currentPath = new java.io.File(".").getCanonicalPath();
System.out.println("Current dir:" + currentPath);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" + currentDir);
输出:
Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32
我的输出不正确,因为C驱动器不是我的当前目录。
如何获取当前目录?
参见:路径操作(Java™教程>基本类>基本I/O)。
使用java.nio.file. path和java.nio.file。路径,您可以执行以下操作来显示Java认为的当前路径。这是7和以上,使用NIO。
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current absolute path is: " + s);
这个输出:
Current absolute path is: /Users/george/NetBeansProjects/Tutorials
对我来说,这就是我上课的地方。
以相对方式构造路径(不使用前分隔符来表示正在构造绝对路径)将使用这个相对路径作为起点。
这是一个非常令人困惑的主题,在提供真正的解决方案之前,我们需要了解一些概念。
File和NIO File Api使用相对路径“”或“。”在内部使用系统参数“user”。Dir”的值来确定返回位置。
“用户。dir”的值基于USER工作目录,该值的行为取决于操作系统和jar的执行方式。
例如,使用文件资源管理器从Linux执行JAR(双击打开它)将设置user。不管jar的位置如何,都可以使用用户的主目录。如果从命令行执行同一个jar,它将返回jar的位置,因为对jar位置的每个cd命令都修改了工作目录。
话虽如此,解决方案采用Java NIO,文件还是“用户”。Dir”属性将以“user. Dir”的方式适用于所有场景。Dir”的值正确。
String userDirectory = System.getProperty("user.dir");
String userDirectory2 = new File("").getAbsolutePath();
String userDirectory3 = Paths.get("").toAbsolutePath().toString();
我们可以使用以下代码:
new File(MyApp.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI().getPath())
.getParent();
来获得已执行JAR的当前位置,我个人使用以下方法来获得预期的位置并覆盖“user”。Dir”的系统属性。所以,以后当使用其他方法时,我总是会得到期望值。
更多细节在这里-> https://blog.adamgamboa.dev/getting-current-directory-path-in-java/
public class MyApp {
static {
//This static block runs at the very begin of the APP, even before the main method.
try{
File file = new File(MyApp.class.getProtectionDomain().getCodeSource()
.getLocation().toURI().getPath());
String basePath = file.getParent();
//Overrides the existing value of "user.dir"
System.getProperties().put("user.dir", basePath);
}catch(URISyntaxException ex){
//log the error
}
}
public static void main(String args []){
//Your app logic
//All these approaches should return the expected value
//regardless of the way the jar is executed.
String userDirectory = System.getProperty("user.dir");
String userDirectory2 = new File("").getAbsolutePath();
String userDirectory3 = Paths.get("").toAbsolutePath().toString();
}
}
我希望这些解释和细节对其他人有帮助…
假设您试图在eclipse或netbean中运行项目,或者在命令行中独立运行项目。我已经写了一个方法来解决它
public static final String getBasePathForClass(Class<?> clazz) {
File file;
try {
String basePath = null;
file = new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
if (file.isFile() || file.getPath().endsWith(".jar") || file.getPath().endsWith(".zip")) {
basePath = file.getParent();
} else {
basePath = file.getPath();
}
// fix to run inside eclipse
if (basePath.endsWith(File.separator + "lib") || basePath.endsWith(File.separator + "bin")
|| basePath.endsWith("bin" + File.separator) || basePath.endsWith("lib" + File.separator)) {
basePath = basePath.substring(0, basePath.length() - 4);
}
// fix to run inside netbean
if (basePath.endsWith(File.separator + "build" + File.separator + "classes")) {
basePath = basePath.substring(0, basePath.length() - 14);
}
// end fix
if (!basePath.endsWith(File.separator)) {
basePath = basePath + File.separator;
}
return basePath;
} catch (URISyntaxException e) {
throw new RuntimeException("Cannot firgue out base path for class: " + clazz.getName());
}
}
要使用,在任何你想要读取文件的基路径的地方,你可以将你的锚类传递给上面的方法,结果可能是你需要的东西:D
最好的