如何从Class文件夹外部读取java中的属性文件?

时间:2023-02-05 13:10:10

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

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

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

这个file_to_read.txt将位于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.

如果config.properties和file_to_read.txt都在src /和mainClass.java中,我可以这样做。但是我想要的方式无法成功。

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.

任何人都可以帮我吗?我需要你的建议,如果我想将myapp文件夹放在我的驱动器中的任何位置,并且我在上面描述的内部结构相同,那么程序将正确完成工作。

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?

我还需要你的建议,如果我想从构建项目后创建的jar中完成这项工作,那么我可以毫无问题地这样做吗?

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!

但是当尝试从属性文件中获取getProperty时,这会给出错误。谢谢!

4 个解决方案

#1


29  

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

如何从Class文件夹外部读取java中的属性文件?

Use FileInputStream with a fixed disk file system path.

将FileInputStream与固定磁盘文件系统路径一起使用。

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:

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

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

or

要么

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

#2


7  

Thanks everybody for your suggestions. I've done the job by this way:

谢谢大家的建议。我通过这种方式完成了这项工作:

        Properties prop = new Properties();
        String dir = System.getProperty("user.dir");
        InputStream in = new FileInputStream(dir + "/myapp/config.properties");
        prop.load(in);
        in.close();
        String filePath = dir + "/myapp/folder_of_file/" + prop.getProperty("file"); /*file contains the file name to read*/

#3


2  

Properties property=new Properties();
property.load(new FileInputStream("C:/java/myapp/config.properties"));

#4


1  

You need to specify absolute path but you shouldn't hard-code it as that will make it harder for switching between development and production environment etc..

您需要指定绝对路径,但不应对其进行硬编码,因为这会使开发和生产环境之间的切换变得更加困难。

You may get the base path of the file(s) from a System property which you can access using System.getProperty("basePath") in your code and which should be prepended to your file name to create an absolute path.

您可以从System属性中获取文件的基本路径,您可以使用代码中的System.getProperty(“basePath”)访问该属性,并且应该在文件名前添加该路径以创建绝对路径。

While running your application you can specify the path in java command line as follows:

在运行应用程序时,您可以在java命令行中指定路径,如下所示:

java -DbasePath="/a/b/c" ...

... signifies the current arguments to your Java command to run your program.

...表示运行程序的Java命令的当前参数。

#1


29  

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

如何从Class文件夹外部读取java中的属性文件?

Use FileInputStream with a fixed disk file system path.

将FileInputStream与固定磁盘文件系统路径一起使用。

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:

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

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

or

要么

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

#2


7  

Thanks everybody for your suggestions. I've done the job by this way:

谢谢大家的建议。我通过这种方式完成了这项工作:

        Properties prop = new Properties();
        String dir = System.getProperty("user.dir");
        InputStream in = new FileInputStream(dir + "/myapp/config.properties");
        prop.load(in);
        in.close();
        String filePath = dir + "/myapp/folder_of_file/" + prop.getProperty("file"); /*file contains the file name to read*/

#3


2  

Properties property=new Properties();
property.load(new FileInputStream("C:/java/myapp/config.properties"));

#4


1  

You need to specify absolute path but you shouldn't hard-code it as that will make it harder for switching between development and production environment etc..

您需要指定绝对路径,但不应对其进行硬编码,因为这会使开发和生产环境之间的切换变得更加困难。

You may get the base path of the file(s) from a System property which you can access using System.getProperty("basePath") in your code and which should be prepended to your file name to create an absolute path.

您可以从System属性中获取文件的基本路径,您可以使用代码中的System.getProperty(“basePath”)访问该属性,并且应该在文件名前添加该路径以创建绝对路径。

While running your application you can specify the path in java command line as follows:

在运行应用程序时,您可以在java命令行中指定路径,如下所示:

java -DbasePath="/a/b/c" ...

... signifies the current arguments to your Java command to run your program.

...表示运行程序的Java命令的当前参数。