title: How to load a properties file from classpath in Java
body: <p>I'm trying to create a config file to save the username and ip data for a client inside the classpath, as in a resources folder in the project. I'm able to retrieve the properties like this:</p>
  
  <pre><code>public String getProperty(String property){
  
      String result = null;
  
      try{
  
          InputStream inputStream = this.getClass().getResourceAsStream(filename);
          Properties properties = new Properties();
  
          if (inputStream != null){
  
              properties.load(inputStream);
              System.out.println(this.getClass().getResource(filename).toString());
  
          }else {
  
              System.out.println("File not found or loaded: "+filename);
  
          }
  
          result = properties.getProperty(property, null);
  
          inputStream.close();
  
      }catch (Exception e){
          e.printStackTrace();            
      }
  
      return result;
  
  }
  </code></pre>
  
  <p>But I also want to be able to set those values which I can get from the file, so to attempt to do that, I use this method:</p>
  
  <pre><code>public void setProperty(String property, String value){
  
      try{
  
          OutputStream out = new FileOutputStream(this.getClass().getResource(filename).getFile());
          Properties properties = new Properties();
  
          properties.setProperty(property, value);
          properties.store(out, "Optional comment");
          out.close();
  
      }catch (Exception e){
  
          System.out.println("Unable to load:"+filename);
          e.printStackTrace();
  
      }
  
  }
  </code></pre>
  
  <p>However, running that method gives me the following error:</p>
  
  <pre><code>java.io.FileNotFoundException: file:/home/andrew/Documents/Programming/Executable-JARs/SchoolClient.jar!/client-config.properties (No such file or directory)
  at java.io.FileOutputStream.open(Native Method)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:221)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:110)
  at com.andrewlalis.utils.DataSaver.setProperty(DataSaver.java:54)
  at com.andrewlalis.MainClient.getNewIp(MainClient.java:173)
  at com.andrewlalis.ClientWindow$4.mouseClicked(ClientWindow.java:142)
  </code></pre>
  
  <p>Now, I have confirmed that the file <code>client-config.properties</code> does exist, as I am able to read data from it, but it seems I'm unable to create an output stream for it. Why is that? Thank you in advance. </p>
  
  <p>The problem I have is that I cannot open an output stream from the file in my classpath.</p>