如何使用相对路径访问txt文件?

huangapple go评论67阅读模式
英文:

How can I access a txt file with a relative path?

问题

我正在使用IntelliJ Idea,我尝试从项目的不同位置读取txt文件。我不想使用像C:/User/...这样的绝对路径,因为如果项目移动到其他位置,项目将无法正常工作。我不确定路径应该是什么样的。

英文:

I'm using IntelliJ Idea and I try to read a txt file from a different location in the project. I don't want to use an absolute path like C:/User/... because if the project is moved to a different location the project wouldn't work anymore. I'm not sure how the path should look like.

答案1

得分: 1

如果您使用相对路径,通常也意味着一旦更改目录结构,它将不再起作用。

示例(我假设这里是Windows):

Java应用程序位于 C:\Tools。您正在使用相对路径(..\file.txt)。这将访问 C:\file.txt.. 移动到上一级目录)。
如果您将该应用程序移动到 C:\Tools\JavaApp,它将不再工作,因为 ..\file.txt 现在指向 C:\Tools\file.txt)。

您有几个选项(我脑海中仅列出了三个选项;有多种方法可以实现您想要的效果):

  1. 使用别名来访问文件。这里 是一些在Windows上可以工作的快捷方式列表(我假设您正在使用Windows)。例如:将文件保存到 %appdata%,它将始终保存到当前用户的应用数据文件夹。要在Java中将Windows快捷方式解析为绝对路径,请使用 System.getenv("APPDATA")

  2. 从程序的工作目录中读取文件
    这可能是您使用相对路径时所指的:
    Java应用程序的当前工作目录是从中运行程序的目录。这将要求您将要读取的文件放在与您的项目/jar文件相同的文件夹中,或者使用相对路径导航到文件的位置,例如您的工作目录中的子文件夹,您将在其中放置该文件。

  3. 使用 .properties 文件声明文件位置
    您还可以从属性文件中读取文件位置,但只能在编译时更改路径。这里有 一篇关于如何使用 .properties 文件的文章。

英文:

If you are using relative Paths, this will generally also mean that it will not work anymore once you are changing directory structure.

Example (I am assuming Windows here):

The java-application resides C:\Tools . You are using a relative Path (..\file.txt). This would access C:\file.txt (.. moves up one directory).
If you moved the App to C:\Tools\JavaApp it wouldn't work anymore, since ..\file.txt now points to C:\Tools\file.txt).

You have several options (three just from the top of my head; there are multiple ways to do this) to achieve what you want:

  1. Use an alias to access the file. Here is a list of windows-shortcuts that will work (I am assuming that you are using Windows). For example: Save the file to %appdata%, and it will always save to the current user's appdata folder. To resolve the windows shortcut to an absolute path in Java, use System.getenv("APPDATA").

  2. Read the file from the program's working directory
    This is probably what you meant by using relative paths:
    The current working directory of a java application is the directory the program is being run from. This will require you to place the file to be read in the same folder as your project/jar-file, or to use relative paths to navigate to the file's location, for example a sub-folder in your working directory where you would place the file.

  3. Use a .properties file to declare the file location
    You could also read the file location from a properties file, however you can only change the path at compile time. Here's an article on how to use .properties-files.

答案2

得分: 1

参见 此链接

您可以像这样使用相对路径访问文件:

File file = new File("..\\..\\file.txt");
String fileAbsolutePath = file.getAbsolutePath();

提示:仅当文件嵌入到项目中时才有效。

英文:

See this

You can access files with relative path like this way:

File file = new File("..\\..\\file.txt");
String fileAbsolutePath = file.getAbsolutePath();

Hint: This only works if the file is embedded to the project.

huangapple
  • 本文由 发表于 2020年8月24日 21:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63561777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定