我有一个属性文件包含文件名称只能说file=fileName.dat
。我将属性文件放在类路径下,并可以从mainClass
中正确读取文件名(file.dat)。在读取文件名后,我将文件名(仅命名为路径)传递给包下的另一个类,例如pack.myClass
来读取该文件。但问题是pack.myClass
无法正确获取文件路径。我已将文件fileName.dat
放在包pack
的内部和外部,但无法使其正常工作。把文件放在java包下的类中读取的位置?
有人可以建议我在哪里放置文件fileName.dat
,这样我就可以正确读取它,整个应用程序也可以随身携带。
谢谢!
我使用读取配置文件和获取文件名中的代码:在myClass
这是在名为pack
我做的包
Properties prop = new Properties();
InputStream in = mainClass.class.getResourceAsStream("config.properties");
prop.load(in);
in.close();
myClass mc = new myClass();
mc.readTheFile(prop.getProperty("file"));
/*until this code is working good*/
然后:
public void readTheFile(String filename) throws IOException {
FileReader fileReader = new FileReader(filename); /*this couldn't get the file whether i'm putting the file inside or outside the package folder */
/*after reading the file I've to do the BufferReader for further operation*/
BufferedReader bufferedReader = new BufferedReader(fileReader);
请向我们提供的代码,以及运行时系统和(Java 2 SE?)指示应用程序的用户 –
向我们显示一些代码。 –
@owlstead:我使用我工作的代码编辑了我的问题 – flyleaf