我的 Java 项目需要使用什么相对路径

发布于 2024-09-26 14:16:37 字数 447 浏览 3 评论 0原文

我的结构如下:

src 
  -|com 
       -|company 
               -|Core --> JAVA class 
  -|rules --> text files

Inside Core is a Java Class that use:

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

但我无法弄清楚要使用什么相对路径。现在我使用:

"..\\..\\..\\..\\rules\\rule1.txt"

我也尝试过:

"../../../../rules/rule1.txt"

我哪里出错了?

I have a structure like:

src 
  -|com 
       -|company 
               -|Core --> JAVA class 
  -|rules --> text files

Inside Core is a Java Class that uses:

BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

But I cannot figure out what relative path to use. Now I use:

"..\\..\\..\\..\\rules\\rule1.txt"

I also tried:

"../../../../rules/rule1.txt"

Where did I go wrong?

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

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

发布评论

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

评论(4

断肠人 2024-10-03 14:16:37

事实并非如此。相对路径是相对于当前应用程序的工作目录而不是类的目录。如果您想了解当前工作目录是什么,请使用 System.getProperty("user.dir")。

更好的方法是将规则目录添加到类路径中,然后使用 Class#getResourceAsStream()


另一种方法是以其他方式设置规则目录的路径:如果可行,您可以提供命令行选项或设置某些系统属性(例如 java -Drules.path=/path/to/rules .. . 并稍后通过 System.getProperty("rules.path") 读取。

It doesn't work that way. Relative paths are relative to the working directory of the current application, not the directory of the class. If you want to find out what your current working directory is, use System.getProperty("user.dir").

A better way is to add the rules directory to the classpath and then use Class#getResourceAsStream().


An alternative would be to set the path to the rules directory somehow else: You could provide a command line option if that is feasible or set some system property (e.g. java -Drules.path=/path/to/rules ... and later read via System.getProperty("rules.path")).

夏九 2024-10-03 14:16:37

这取决于您从哪里启动,而不是 Java 类的包结构。正如 @EJP 指出的那样,还有其他 API 确实反映了您的包结构。

我过去使用过的一个技巧是让一行代码创建一个文件“SomeSillyName.txt”。看看它实际上在哪里出现。

我的猜测是,您可能从 src 启动,因此简单的“rules/rule1.txt”就可以了。

It depends where you are launching from, not on the package structure of your java classes. As @EJP points out there are other APIs that do reflect your package structure.

One trick I have used in the past is to have a line of code create a file "SomeSillyName.txt". And see where that actually pops up.

My guess is that you might be launching from src, and so a simple "rules/rule1.txt" would work.

乖乖哒 2024-10-03 14:16:37

执行时的当前目录与当前类所在的包无关,不会改变。

如果您的文件随应用程序一起分发,请使用 Class.getResourceAsStream()。

The current directory when executing has nothing to do with what package the current class is in. It doesn't change.

If your file is distributed with the application, use Class.getResourceAsStream().

ヤ经典坏疍 2024-10-03 14:16:37

如果您使用 Eclipse 等 IDE,则工作目录默认为项目根文件夹。所以 src/rules/rule1.txt 应该可以工作。 (或者我误解了你的文件夹结构。)

If you're using an IDE like Eclipse the working directory is by default the projects root folder. So src/rules/rule1.txt should work. (Or I misunderstood your folder structure.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文