如何从Class文件夹外部读取java中的属性文件? [英] How to read a properties file in java from outside the Class folder?

查看:131
本文介绍了如何从Class文件夹外部读取java中的属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  D:/java/myapp/src/manClass.java 

我想把属性文件放在$ / b>

  D:/java/myapp/config.properties 

这将包含一个文件名和其他一些配置。我将在属性文件中设置文件名如下: file = file_to_read.txt



this file_to_read.txt 将位于 D:/ java / myapp / folder_of_file /



主类将首先从属性文件中读取文件名,然后从文件中获取内容。



如果 config.properties file_to_read.txt 位于 src / code> mainClass.java 。但是不能用我想要的方式取得成功。



有人可以帮我吗?我需要你的建议,如果我想把我的驱动器中的 myapp 文件夹放在我上面描述的相同内部结构中的任何地方,程序将执行

我还需要你的建议,如果我想从构建项目后创建的jar中完成这项工作,那么我可以这样做没有任何问题吗? p>

我试过以下的方法来读取属性文件:

  URL location = myClass.class.getProtectionDomain()。getCodeSource()。getLocation(); 
$ b $ String filePath = location.getPath()。substring(1,location.getPath()。length());

InputStream in = myClass.class.getResourceAsStream(filePath +config.properties);
prop.load(in);

in.close();

System.out.println(prop.getProperty(file));

但是,当从属性文件尝试getProperty时会产生错误。
Thanks!

解决方案


Class文件夹之外

使用 FileInputStream


$ b

  InputStream input = new FileInputStream(D:/ java / myapp / config.properties); 

更好的方法是将其移动到classpath覆盖的现有路径之一,或者将其原始路径 D:/ java / myapp / 添加到类路径中。然后你可以得到如下:

pre $ InputStream input = getClass()。getResourceAsStream(/ config.properties);

  InputStream input = Thread.currentThread()。getContextClassLoader()。getResourceAsStream(config.properties); 


I have the following path structure to the main class:

D:/java/myapp/src/manClass.java

and i want to put the properties file in

D:/java/myapp/config.properties

which will contain a file name and some other configurations. I'll set the file name in properties file like this: file=file_to_read.txt

this file_to_read.txt will be located in D:/java/myapp/folder_of_file/

The main class will read the file name from the properties file first and then get the contents form the file.

I can do this if both config.properties and file_to_read.txt are in src/ with the mainClass.java. But could not succeed with the way I want to do it.

Can anybody help me with this? I need your suggestion about what can I do if I want to put the myapp folder anywhere in my drive with the same internal structure in it I described above and the program will do the job correctly.

I also need your suggestion that if I want to do the job from the jar created after building the project then can I do that without any problem?

I've tried as the following just to read the properties file:

        URL location = myClass.class.getProtectionDomain().getCodeSource().getLocation();

        String filePath = location.getPath().substring(1,location.getPath().length());

        InputStream in = myClass.class.getResourceAsStream(filePath + "config.properties");
        prop.load(in);

        in.close();

        System.out.println(prop.getProperty("file"));

But this gives err when tried to getProperty from the properties file. Thanks!

解决方案

How to read a properties file in java from outside the Class folder?

Use FileInputStream with a fixed disk file system path.

InputStream input = new FileInputStream("D:/java/myapp/config.properties");

Much better is to just move it to one of the existing paths covered by the classpath instead, or to add its original path D:/java/myapp/ to the classpath. Then you can get it as follows:

InputStream input = getClass().getResourceAsStream("/config.properties");

or

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");

这篇关于如何从Class文件夹外部读取java中的属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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