简体   繁体   中英

Write to Properties File in Java without knowing it's path

I am loading my properties file using the class loader as follows.

Properties prop = new Properties();
prop.load(MyClass.class.getResourseAsStream("/Property.properties"));

Now, using this method I am able to read the properties file. I want to write some data to the property file. I don't know the path of the property file. How do I store the data to the property file then ?

Update

I tried the following, but it doesn't give me the correct path:

File propFile = new File("Property.properties");
System.out.println(propFile.getAbsolutePath());

I don't think you can in a generic way that would always work, because your properties file could be bundled inside a jar, etc. You can get the URL via getResource(String) and then do something with that URL, for example if it's a file URL, you could get the file name there.

 URL u=MyClass.class.getResource("/Property.properties");
 if ("file".equals(u.getProtocol()){
    File f=new File(u.toURI());
 }

But that wouldn't work in all cases.

I would write the modified value to a file in a well known location, and use the bundled Properties as the default value, that are overriden by the values in the file.

There are two parts to your question.

First, the reading part. You said: "I am loading my properties file ..." using the code you provided. This code treats the file as a "resource" and loads it using the class loader. The class loader mechanism in the Java Runtime comes into picture here. Roughly speaking, this mechanism searches for it in your application's current classpath and makes the input stream associated with first matching resource available to your code. It may be fine in your case, however, you should realize that if there are multiple properties files by the same name in different parts of your classpath (eg different JAR files having the same config file), then you may not know which file is being read. The only way to ensure that you are reading the right file from the classpath is to ensure that you have that config file in a unique place in your application's classpath.

This seems to work for you. Reading from a file is easier, than, say writing to a file.

In the second part, you want to write to a file . It's important to note that you should know the exact whereabouts of the file you are writing to. If you rather unknowingly convert it to an output stream, then you might end up trying to write to a stream that you are not allowed to write to. So, here, you should try to find the location (path) of the actual, physical file, for example, on a computer's hard drive. That path is something you should know before you write and run this program. Remember that a file always exists in a folder or a directory.

Your attempt:

File propFile = new File("Property.properties");
System.out.println(propFile.getAbsolutePath());

to find the location or path of the file you want to write to fails because when you do new File("Property.properties"); , Java tries to refer to a file (does not do anything yet with the operating system) that resides in the Java Runtime's current working directory. This refers to the location given by System.getProperty("user.dir") . So, your propFile refers to a (hypothetical) file in that folder and the call to getAbsolutePath() prints its path. This is not the file that you want because you are trying to write to a file whose path you don't know. So, you should find the path of the file and then use a BufferedWriter backed by a FileWriter or BufferedOutputStream backed by a FileOutputStream to write to this file. Make sure that the file you want to write to exists and you have permission to write to it.

Essentially get the resource as an OutputStream, then pass it to the store() method of your Properties object.

Details here:

http://www.drdobbs.com/jvm/readwrite-properties-files-in-java/231000005

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM