Qaz*_*azi 955 java working-directory
我想使用访问我当前的工作目录
String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
Run Code Online (Sandbox Code Playgroud)
输出:
Current dir: C:\WINDOWS\system32
Current dir using System: C:\WINDOWS\system32
Run Code Online (Sandbox Code Playgroud)
我的输出不正确,因为C盘不是我当前的目录.在这方面需要帮助.
Anu*_*tel 1089
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Working Directory = " +
System.getProperty("user.dir"));
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印出初始化应用程序的完整绝对路径.
小智 362
请参阅:http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html
使用java.nio.file.Path和java.nio.file.Paths,您可以执行以下操作以显示Java认为您当前的路径.这适用于7及以上,并使用NIO.
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
Run Code Online (Sandbox Code Playgroud)
这输出Current relative path is: /Users/george/NetBeansProjects/Tutorials在我的情况下是我运行类的地方.以相对方式构造路径,通过不使用前导分隔符来指示您正在构建绝对路径,将使用此相对路径作为起点.
Dmi*_*lov 220
以下适用于Java 7及更高版本(有关文档,请参见此处).
import java.nio.file.Paths;
Paths.get(".").toAbsolutePath().normalize().toString();
Run Code Online (Sandbox Code Playgroud)
Mar*_*ark 60
这将为您提供当前工作目录的路径:
Path path = FileSystems.getDefault().getPath(".");
Run Code Online (Sandbox Code Playgroud)
这将为您提供工作目录中名为"Foo.txt"的文件的路径:
Path path = FileSystems.getDefault().getPath("Foo.txt");
Run Code Online (Sandbox Code Playgroud)
编辑: 从文件系统的根目录获取当前目录的绝对路径:
Path path = FileSystems.getDefault().getPath(".").toAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
小智 33
这是我的解决方案
File currentDir = new File("");
Run Code Online (Sandbox Code Playgroud)
fre*_*dev 27
我在评论中发现这个解决方案比其他更好,更便携:
String cwd = new File("").getAbsolutePath();
Run Code Online (Sandbox Code Playgroud)
Ami*_*aei 25
这在JAR文件中也可以正常工作.您可以获得CodeSource通过ProtectionDomain#getCodeSource()并ProtectionDomain依次获得人Class#getProtectionDomain().
public class Test {
public static void main(String... args) throws Exception {
URL location = Test.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
}
}
Run Code Online (Sandbox Code Playgroud)
Pet*_*ter 22
this.getClass().getClassLoader().getResource("").getPath()
Run Code Online (Sandbox Code Playgroud)
com*_*ome 17
通常,作为File对象:
File getCwd() {
return new File("").getAbsoluteFile();
}
Run Code Online (Sandbox Code Playgroud)
你可能希望拥有像"D:/ a/b/c"这样的完全限定字符串:
getCwd().getAbsolutePath()
Run Code Online (Sandbox Code Playgroud)
Boh*_*dan 12
我在Linux上并且对这两种方法都得到了相同的结果:
@Test
public void aaa()
{
System.err.println(Paths.get("").toAbsolutePath().toString());
System.err.println(System.getProperty("user.dir"));
}
Run Code Online (Sandbox Code Playgroud)
System.getProperty("user.dir") 文档
我希望您要访问包含包的当前目录,即如果您的 Java 程序在其中c:\myApp\com\foo\src\service\MyTest.java并且您想打印到c:\myApp\com\foo\src\service那时您可以尝试以下代码:
String myCurrentDir = System.getProperty("user.dir")
+ File.separator
+ System.getProperty("sun.java.command")
.substring(0, System.getProperty("sun.java.command").lastIndexOf("."))
.replace(".", File.separator);
System.out.println(myCurrentDir);
Run Code Online (Sandbox Code Playgroud)
注意:此代码仅在带有 Oracle JRE 的 Windows 中进行了测试。
小智 5
在Linux 上,当您从终端运行jar文件时,无论您的 jar 文件在哪里,它们都会返回相同的信息:“/home/CurrentUser”。当您启动 jar 文件时,这仅取决于您在终端中使用的当前目录。String
Paths.get("").toAbsolutePath().toString();
System.getProperty("user.dir");
Run Code Online (Sandbox Code Playgroud)
如果您的Classwithmain将被调用MainClass,请尝试:
MainClass.class.getProtectionDomain().getCodeSource().getLocation().getFile();
Run Code Online (Sandbox Code Playgroud)
这将返回一个String带有绝对路径的的JAR文件。
当前工作目录在不同的Java实现中的定义不同.对于Java 7之前的某个版本,没有一致的方法来获取工作目录.您可以通过启动Java文件-D并定义一个变量来保存信息来解决这个问题
就像是
java -D com.mycompany.workingDir="%0"
Run Code Online (Sandbox Code Playgroud)
这不太对,但你明白了.那么System.getProperty("com.mycompany.workingDir")......
| 归档时间: |
|
| 查看次数: |
1247775 次 |
| 最近记录: |