从类路径位置读取文件以用于“当前项目”。 [英] Reading file from classpath location for "current project"
问题描述
我有两个月食项目A和B。 B具有对A的依赖性。在项目A中是一个文本文件 file.txt
。此文件可以通过例如 getClass()。getResourceAsStream(...)
...加载到项目B中,因为文件的位置在 classpath
。我想在项目B中提供相同的文件名,并且仅在项目B中存在文件时才执行某些操作。如果项目B中没有文件,则在使用 getClass()加载期间应该找不到文件。 getResourceAsStream(...)
。
i have 2 projects A and B in eclipse. B has dependency to A. In project A is a text file "file.txt"
. This file can be loaded in project B by for example getClass().getResourceAsStream(...)
... because the location of the file is in classpath
. I want to provide this same file name in project B and do some action only if file exists in project B. If there is no file in project B then no file should be found during loading with getClass().getResourceAsStream(...)
. Currently the file will be always found.
谢谢您的帮助。
亲切的问候
塞巴斯蒂安
Kind regards Sebastian
我需要提供有关该问题的更多信息。抱歉。
I need to provide a little bit more information about the problem. Sorry.
有一个项目C是核心。在项目C中,有一个执行以下操作的类:
There is a project C which is the core. In project C there is class which do the following:
- 检查是否
file.txt
存在。这是仅检查文件名的基本代码。没有包裹名称。而且我也不想更改它。 - 如果是,那就做些事情。
- 如果现在再做点其他事情。
- Check if
file.txt
exists. This is the base code which checks only the file name. No package name. And i dont want to change it. - if yes then do something.
- if now then do something else.
A依赖于C,B也依赖于C。如果将执行A,则将尝试从C的基本代码中查找文件.txt
。
A depends on C and B also depends on C. If A will be executed then the base code from C will be try to find file.txt
.
如果将执行B,则还将尝试查找基本代码 file.txt
。如果B项目中没有文件,则会找到它,因为它在A中。我不想要它。
If B will be executed then the base code will be also try to find file.txt
. If there is no file in B project then it will found because it is in A. I dont want it.
非常感谢您。
推荐答案
在项目B中的文件添加到其他类路径位置,您首先尝试在项目B中的位置,然后检查项目A中的文件。在项目B中是com.myPackage.file1,在项目A中是com.myPackage.projA.file1。
Add the file in project B in a different classpath location, you try first the location in project B and then check the file in project A. Say the location is com.myPackage.file1 in project B and com.myPackage.projA.file1 in project A.
这篇关于从类路径位置读取文件以用于“当前项目”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!