Reading file from classpath location for “current project”

孤街醉人 提交于 2019-12-11 01:34:24

问题


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.

Thank you for help.

Kind regards Sebastian


I need to provide a little bit more information about the problem. Sorry.

There is a project C which is the core. In project C there is class which do the following:

  1. 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.
  2. if yes then do something.
  3. if now then do something else.

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.

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.

Thank your very much.


回答1:


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.



来源:https://stackoverflow.com/questions/5257201/reading-file-from-classpath-location-for-current-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!