0

Trying to write a value to a parameter in Properties file. The file config.properties is already created under /src/config folder and I am able to read it. It has only one parameter "status". But when I try to update/write to the property file, getting the following error: java.io.FileNotFoundException: \src\config\config.properties (The system cannot find the path specified)

Properties prop = new Properties();
    prop.setProperty("status","1");
        try {
            prop.store(new FileOutputStream("/src/config/config.properties"), null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
dorj bayar
  • 53
  • 8
  • 1
    Are you sure that you want an absolute path starting at / ??? In a unix-like file system that / is the root of everything, and it doesnt come as surprise that you dont have write permissions. In other words: please read [mcve] and dont tell us "ha, this other code works". Instead, show all parts that are relevant. – GhostCat Jun 15 '19 at 13:05
  • The following code works for reading the properties file. I want to write to the file. Properties prop = new Properties(); InputStream is = null; is=this.getClass().getResourceAsStream("/config/config.properties"); try { prop.load(is); } catch (IOException ex) { } String prevstatus=prop.getProperty("status"); try { is.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } – dorj bayar Jun 15 '19 at 13:13
  • Can or would you want to read source code forced into comments? Nope. So please edit your question instead and make sure all relevant information is included in the question! – GhostCat Jun 15 '19 at 13:14
  • 1
    (1) You're reading the properties file as a _resource_ but trying to write to it as an _external file_. (2) Resources are typically read-only. – Slaw Jun 15 '19 at 13:27

2 Answers2

3

Here's the problem:

    is=this.getClass().getResourceAsStream("/config/config.properties");

This is not as normal file system read. It is reading a resource from the classpath using a path that is relative a directory or JAR file that is on the classpath. The path is a resource path, not a file system path.

If you want to write to that file:

  1. It must be a free-standing file, not a JAR or ZIP file entry or something like that.

  2. It mustn't be read-only ... or locked.

  3. You need to know the actual absolute or relative file system pathname that corresponds to the resource path. (Because the resource API methods don't provide a way to open a resource for writing.)

Instead, you have tried to use the resource path to identify the file when you open it to write. (The FileOutputStream class requires a file system path.)


Basically, you are going to have to redesign this. Updating things on the resource path is typically difficult, and sometimes impossible. Also, you appear to be refering to the resource in a /src/... location, which won't be on the classpath when your application is deployed. (I assume that you won't expect your users to download, install and learn to use a Java IDE so that they can run your app!)

A better idea is to put the properties file into the application's installation directory, or the user's home directory. Then you figure out a way for the application to find out where that is. (For example, if you are launching the app using a wrapper script, the script could pass the installation directory path to your app using a -D option.

Another alternative is to use the Java settings API which can store the settings in a (platform specific) standard place in the file system.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

I need the configuration construction system, you use a gradle, maven, ant? nothing?

Because your code will do not work into the jar because into jar not exist the src directory.

with your information, I say with the absolute path you resolve your problem but is bad because you coupling with a file system structure.

For using absolute path look this post, but I hope you add more information at the post for your project.

prop.store(new FileOutputStream(System.getProperty("user.dir") + "/src/config/config.properties"), null);

I looked a comment and your read the proprietis file with this.getClass().getResourceAsStream() and it have a different path resolution the FileOutputStream.

For resolve the problem we will have to need more information for the structure of the project, please modify your question because is generic as my solution to use the absolute path

vincenzopalazzo
  • 1,487
  • 2
  • 7
  • 35
  • I am using Eclipse,plus no any build system. It is a Java web service. Trying to avoid absolute path. this.getClass().getResourceAsStream() is working nicely to read the properties file. But I am not able to use it to write to the property file. For the original config file, I created from Eclipse by File->New and gave only parameter status=0. – dorj bayar Jun 15 '19 at 13:29
  • Now error message: java.io.FileNotFoundException: C:\Users\JohnWilson\Desktop\src\config\config.properties (The system cannot find the path specified) – dorj bayar Jun 15 '19 at 13:36
  • For write file into web app is more different to write file into desktop file, look this [discussion](https://serverfault.com/questions/156759/where-to-write-files-form-a-web-application-running-on-tomcat-on-ubuntu), if you not adding more information to the post we cant not did help you – vincenzopalazzo Jun 15 '19 at 13:36