Java中如何获取当前工作目录?
假设我的主类位于 C:\Users\Justian\Documents\ 中。如何让我的程序显示它位于 C:\Users\Justian\Documents
中?
硬编码不是一种选择——如果它被移动到另一个位置,它需要适应。
我想将一堆 CSV 文件转储到一个文件夹中,让程序识别所有文件,然后加载数据并操作它们。我真的只是想知道如何导航到该文件夹。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一种方法是使用 系统属性
System.getProperty("user.dir");
这将为您提供“初始化属性时的当前工作目录”。这可能就是您想要的。找出发出 java 命令的位置,在您的情况下是在包含要处理的文件的目录中,即使实际的 .jar 文件可能驻留在计算机上的其他位置。在大多数情况下,拥有实际 .jar 文件的目录并没有多大用处。以下内容将打印出调用命令的当前目录,无论 .class 文件位于 .class 或 .jar 文件中的位置。
如果您位于
/User/me/
且您的 .jar包含上述代码的文件位于/opt/some/nested/dir/
命令
java -jar /opt/some/nested/dir/test.jar Test
将输出current dir = /User/me
。作为奖励,您还应该考虑使用良好的面向对象命令行参数解析器。
我强烈推荐 JSAP,Java 简单参数解析器。这将允许您使用 System.getProperty("user.dir") ,或者传递其他内容来覆盖该行为。一个更易于维护的解决方案。这将使传入要处理的目录变得非常容易,并且如果没有传入任何内容,则能够依靠
user.dir
。One way would be to use the system property
System.getProperty("user.dir");
this will give you "The current working directory when the properties were initialized". This is probably what you want. to find out where thejava
command was issued, in your case in the directory with the files to process, even though the actual .jar file might reside somewhere else on the machine. Having the directory of the actual .jar file isn't that useful in most cases.The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in.
if you are in
/User/me/
and your .jar file containing the above code is in/opt/some/nested/dir/
the command
java -jar /opt/some/nested/dir/test.jar Test
will outputcurrent dir = /User/me
.You should also as a bonus look at using a good object oriented command line argument parser.
I highly recommend JSAP, the Java Simple Argument Parser. This would let you use
System.getProperty("user.dir")
and alternatively pass in something else to over-ride the behavior. A much more maintainable solution. This would make passing in the directory to process very easy to do, and be able to fall back onuser.dir
if nothing was passed in.使用
CodeSource#getLocation ()
。这在 JAR 文件中也能正常工作。您可以通过 CodeSource ="noreferrer">ProtectionDomain#getCodeSource()
和ProtectionDomain
依次可以通过Class#getProtectionDomain()
。根据OP的评论更新:
这需要硬编码/知道它们在程序中的相对路径。而是考虑将其路径添加到类路径中,以便您可以使用 ClassLoader#getResource()
或将其路径作为 main() 参数传递。
Use
CodeSource#getLocation()
. This works fine in JAR files as well. You can obtainCodeSource
byProtectionDomain#getCodeSource()
and theProtectionDomain
in turn can be obtained byClass#getProtectionDomain()
.Update as per the comment of the OP:
That would require hardcoding/knowing their relative path in your program. Rather consider adding its path to the classpath so that you can use
ClassLoader#getResource()
Or to pass its path as
main()
argument.打印如下内容:
请注意,
File.getCanonicalPath()
会抛出一个已检查的 IOException,但它将删除诸如../../../
之类的内容Prints something like:
Note that
File.getCanonicalPath()
throws a checked IOException but it will remove things like../../../
如果您想获取当前工作目录,请使用以下行
If you want to get your current working directory then use the following line
我刚刚用过:
...
I just used:
...
如果你想要当前源代码的绝对路径,我的建议是:
If you want the absolute path of the current source code, my suggestion is:
谁说你的主类位于本地硬盘上的文件中?类通常捆绑在 JAR 文件中,有时通过网络加载,甚至动态生成。
那么你真正想做的是什么?可能有一种方法可以做到这一点,而无需假设类的来源。
Who says your main class is in a file on a local harddisk? Classes are more often bundled inside JAR files, and sometimes loaded over the network or even generated on the fly.
So what is it that you actually want to do? There is probably a way to do it that does not make assumptions about where classes come from.