java中如何指定文件路径?

发布于 2024-08-10 13:35:47 字数 206 浏览 8 评论 0原文

我已经为“Debian Linux”创建了一个java应用程序。现在我希望该应用程序读取放置在指定该应用程序的 jar 文件的目录中的文件。那么在文件对象的参数中要指定什么?

File fileToBeReaded = new File(...);

为上述语句指定什么参数来指定表示应用程序的 jar 文件放置路径的相对文件路径?

I have created a java application for "Debian Linux." Now I want that that application reads a file placed in the directory where the jar file of that application is specified. So what to specify at the argument of the File Object?

File fileToBeReaded = new File(...);

What to specify as argument for the above statement to specify relative filepath representing the path where the jar file of the application has been placed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

独木成林 2024-08-17 13:35:47

您是在问转义字符问题吗?

如果是这种情况,请使用正斜杠而不是反斜杠,例如

“C:/Users/You/Desktop/test.txt”

而不是

“C:\Users\You\Desktop\test.txt”

Are you asking about escape character issues?

If that is the case then use forward slashes instead of backward slashes like

"C:/Users/You/Desktop/test.txt"

instead of

"C:\Users\You\Desktop\test.txt"

滴情不沾 2024-08-17 13:35:47

如果你知道文件名,当然就简单了。

new File("./myFileName")

如果你不知道文件名,你可以使用 File 对象的 list() 方法来获取当前目录下的文件列表,然后选择你想要的文件。

If you know the name of the file, of course it's simply

new File("./myFileName")

If you don't know the name, you can use the File object's list() method to get a list of files in the current directory, and then pick the one you want.

无戏配角 2024-08-17 13:35:47

在 java.io.File 中使用相对路径完全依赖于当前工作目录。这与执行 JAR 的方式不同。例如,如果您在 /foo 中并通过 java -jar /bar/jar/Bar.jar 执行 JAR,那么工作目录仍然是 / foo.但是如果您 cd/bar/jar 并执行 java -jar Bar.jar 那么工作目录是 /bar/jar

如果您想要 JAR 所在的根路径,方法之一是:

File root = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI());

这返回 JAR 文件的根路径(包含类路径根)。如果您将资源相对于类路径根放置,则可以按如下方式访问它:

File resource = new File(root, "filename.ext");

或者您也可以使用:

File resource = new File(Thread.currentThread().getContextClassLoader().getResource("filename.ext").toURI());

Using relative paths in java.io.File is fully dependent on the current working directory. This differs with the way you execute the JAR. If you're for example in /foo and you execute the JAR by java -jar /bar/jar/Bar.jar then the working directory is still /foo. But if you cd to /bar/jar and execute java -jar Bar.jar then the working directory is /bar/jar.

If you want the root path where the JAR is located, one of the ways would be:

File root = new File(Thread.currentThread().getContextClassLoader().getResource("").toURI());

This returns the root path of the JAR file (i.o.w. the classpath root). If you place your resource relative to the classpath root, you can access it as follows:

File resource = new File(root, "filename.ext");

Alternatively you can also just use:

File resource = new File(Thread.currentThread().getContextClassLoader().getResource("filename.ext").toURI());
终难愈 2024-08-17 13:35:47

我认为这应该可以解决问题:

File starting = new File(System.getProperty("user.dir"));
File fileToBeRead = new File(starting,"my_file.txt");

这样,将在 user.dir 属性中搜索该文件,这将是您应用程序的工作目录。

I think this should do the trick:

File starting = new File(System.getProperty("user.dir"));
File fileToBeRead = new File(starting,"my_file.txt");

This way, the file will be searched in the user.dir property, which will be your app's working directory.

南七夏 2024-08-17 13:35:47

您可以要求您的类加载器为您提供 jar 的位置:

getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

...但我建议将您要查找的文件放入 jar 文件中并将其作为资源读取 (getClass().getResourceAsStream( “myFile.txt”))。

You could ask your classloader to give you the location of the jar:

getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

...but I'd suggest to put the file you are looking for inside your jar file and read it as a resource (getClass().getResourceAsStream( "myFile.txt" )).

时光病人 2024-08-17 13:35:47

在 IntelliJIDEA 上右键单击该文件,然后复制绝对路径,然后在双引号中将路径粘贴为 filePath。
例如它应该是这样的:

"C:\\Users\\NameOfTheComputerUser\\IdeaProjects\\NameOfTheProject\\YourSubFolders\\name-of-the-file.example"

On IntelliJIDEA right click on the file then copy the absolute path, then in the double quotation paste the path as filePath.
for example it should be something like this:

"C:\\Users\\NameOfTheComputerUser\\IdeaProjects\\NameOfTheProject\\YourSubFolders\\name-of-the-file.example"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文