在Java中的包下的文件从哪里读取? [英] Where to put a file to read from a class under a package in java?

查看:575
本文介绍了在Java中的包下的文件从哪里读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件名的属性文件,只能说 file = fileName.dat 。我把属性文件放在类路径下,可以在 mainClass 中正确读取文件名(file.dat)。读取文件名后,我将文件名(只是不命名的路径)传递给包下的另一个类 pack.myClass 来读取该文件。但问题是 pack.myClass 无法正确获取文件路径。我已经将文件 fileName.dat 放在包 pack 的内部和外部,但是无法工作。

任何人都可以建议我把文件放在哪里 fileName.dat ,所以我可以正确地读取它,应用程序也是可移植的。

谢谢!

我用来读取配置文件并获取文件名的代码:

 属性prop = new Properties(); 
InputStream in = mainClass.class.getResourceAsStream(config.properties);
prop.load(in);
in.close();

myClass mc = new myClass();
mc.readTheFile(prop.getProperty(file));
/ *直到此代码运行良好* /

然后在 myClass 它位于包名为 pack 的包下面:

  public void readTheFile(String filename)throws IOException {
FileReader fileReader = new FileReader(filename); / *这是不能得到的文件,无论我把文件内部或外部的包文件夹* /
/ *读取文件后,我已经做了进一步操作的BufferReader * /
BufferedReader bufferedReader = new BufferedReader(fileReader);


解决方案

我假定您正在尝试读取属性文件类的getResource方法。如果将属性文件放在类路径的根目录下,则应在文件名前加上/来表示类路径的根目录,例如getResource(/ file.dat)。如果属性文件与您在其上调用getResource方法的类位于同一文件夹下,则不应使用/前缀。

I have a properties file contains the file name only say file=fileName.dat. I've put the properties file under the class path and could read the file name(file.dat) properly from it in the mainClass. After reading the file name I passed the file name(just name not the path) to another class under a package say pack.myClass to read that file. But the problem is pack.myClass could not get the file path properly. I've put the file fileName.dat both inside and outside the packagepack but couldn't make it work.

Can anybody suggest me that where to put the file fileName.dat so I can read it properly and the whole application would be portable too.

Thanks!

The code I'm using to read the config file and getting the file name:

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*/

Then in myClass which is under package named pack I am doing:

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);

解决方案

I assume that you are trying to read properties file using getResource method of class. If you put properties file on root of the classpath you should prefix file name with '/' to indicate root of classpath, for example getResource("/file.dat"). If properties file is under the same folder with the class you on which you invoke getResource method, than you should not use '/' prefix.

这篇关于在Java中的包下的文件从哪里读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆