I need to overwrite the value in property file





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-3















My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..



my property file Location packagName->resource->folderName->.propertyFile



property File: I need to overwrite the value for "epochFromTime":



FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862


Java Code:



try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}









share|improve this question


















  • 5





    What have you tried and what isn't working?

    – Zephyr
    Jan 4 at 15:47











  • No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

    – Bilbo
    Jan 4 at 15:52











  • There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

    – jhamon
    Jan 4 at 15:56








  • 3





    Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

    – Zephyr
    Jan 4 at 15:56











  • You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

    – f1sh
    Jan 4 at 15:59


















-3















My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..



my property file Location packagName->resource->folderName->.propertyFile



property File: I need to overwrite the value for "epochFromTime":



FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862


Java Code:



try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}









share|improve this question


















  • 5





    What have you tried and what isn't working?

    – Zephyr
    Jan 4 at 15:47











  • No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

    – Bilbo
    Jan 4 at 15:52











  • There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

    – jhamon
    Jan 4 at 15:56








  • 3





    Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

    – Zephyr
    Jan 4 at 15:56











  • You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

    – f1sh
    Jan 4 at 15:59














-3












-3








-3








My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..



my property file Location packagName->resource->folderName->.propertyFile



property File: I need to overwrite the value for "epochFromTime":



FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862


Java Code:



try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}









share|improve this question














My property file contains 3 properties I need to overwrite the value for thirdOne. How do I load the property file from class path in my java code and overwrite it..



my property file Location packagName->resource->folderName->.propertyFile



property File: I need to overwrite the value for "epochFromTime":



FILE_PATH=C:\Users\pda\Desktop\JsonOutput\DataExtract
epochFilename=C:\Users\pda\Desktop\JsonOutput\epochTime.txt
epochFromTime=1545329531862


Java Code:



try {
Properties config = new Properties();
config.load(ClassLoader.getSystemResourceAsStream(PROPERTIES_PATH));
String epochFromTimeChanged= Long.toString(epoch_to2);
config.setProperty("epochFromTime",epochFromTimeChanged);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}






java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 15:45









BilboBilbo

1115




1115








  • 5





    What have you tried and what isn't working?

    – Zephyr
    Jan 4 at 15:47











  • No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

    – Bilbo
    Jan 4 at 15:52











  • There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

    – jhamon
    Jan 4 at 15:56








  • 3





    Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

    – Zephyr
    Jan 4 at 15:56











  • You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

    – f1sh
    Jan 4 at 15:59














  • 5





    What have you tried and what isn't working?

    – Zephyr
    Jan 4 at 15:47











  • No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

    – Bilbo
    Jan 4 at 15:52











  • There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

    – jhamon
    Jan 4 at 15:56








  • 3





    Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

    – Zephyr
    Jan 4 at 15:56











  • You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

    – f1sh
    Jan 4 at 15:59








5




5





What have you tried and what isn't working?

– Zephyr
Jan 4 at 15:47





What have you tried and what isn't working?

– Zephyr
Jan 4 at 15:47













No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

– Bilbo
Jan 4 at 15:52





No it is not working. Can u please guide how do I make a use of FileOutputStream to do this?

– Bilbo
Jan 4 at 15:52













There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

– jhamon
Jan 4 at 15:56







There are many examples on the usage of OutputStream on SO. I'm sure you can easily find them. For properties, if you cna use external libraries, Apache Commons Configuration is made speciffcally to manage them.

– jhamon
Jan 4 at 15:56






3




3





Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

– Zephyr
Jan 4 at 15:56





Just saying "it is not working" does not help us in any way. Please check out the How to Ask article and post a Minimal, Complete, and Verifiable example that demonstrates the exact problem you're facing...

– Zephyr
Jan 4 at 15:56













You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

– f1sh
Jan 4 at 15:59





You are only loading your properties from a file, then change the value in memory. Your code does not save anything to a file and there is no reason to assume this just magically happens. You have to try something, not say "it's not working".

– f1sh
Jan 4 at 15:59












2 Answers
2






active

oldest

votes


















0














This is pretty easy. First do read a property file to Properties. Then update it and save it afterwards. Do not forget to close resources.



public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
Properties properties = new Properties();

try (Reader reader = new BufferedReader(new FileReader(file))) {
properties.load(reader);
}

consumer.accept(properties);

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
properties.store(writer, "comment");
}

}




Client code could look like this:



updateProperties(
new File("application.properties"),
properties -> {
properties.setProperty("two", "two_two_two");
properties.setProperty("three", "three_three");
});




application.properties



before update



two=two_two
one=one_one


after update



#comment
#Fri Jan 04 18:59:12 MSK 2019
two=two_two_two
one=one_one
three=three_three





share|improve this answer































    0














    Use Properties.store() to write the changed value back to the file:



    String PROPERTIES_PATH = "...";
    try {
    File f = new File(PROPERTIES_PATH);
    FileInputStream in = new FileInputStream(f);
    Properties config = new Properties();
    config.load(in);

    String epochFromTimeChanged= Long.toString(epoch_to2);
    config.setProperty("epochFromTime",epochFromTimeChanged);

    // get or create the file
    File f = new File(PROPERTIES_PATH);
    OutputStream out = new FileOutputStream(f);
    config.store(out, "My properties file comment");

    } catch (FileNotFoundException e1) {
    log.info("{} does not exist", PROPERTIES_PATH);
    } catch (IOException e) {
    log.error("Cannot access {}", PROPERTIES_PATH, e);
    }





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042114%2fi-need-to-overwrite-the-value-in-property-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This is pretty easy. First do read a property file to Properties. Then update it and save it afterwards. Do not forget to close resources.



      public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
      Properties properties = new Properties();

      try (Reader reader = new BufferedReader(new FileReader(file))) {
      properties.load(reader);
      }

      consumer.accept(properties);

      try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
      properties.store(writer, "comment");
      }

      }




      Client code could look like this:



      updateProperties(
      new File("application.properties"),
      properties -> {
      properties.setProperty("two", "two_two_two");
      properties.setProperty("three", "three_three");
      });




      application.properties



      before update



      two=two_two
      one=one_one


      after update



      #comment
      #Fri Jan 04 18:59:12 MSK 2019
      two=two_two_two
      one=one_one
      three=three_three





      share|improve this answer




























        0














        This is pretty easy. First do read a property file to Properties. Then update it and save it afterwards. Do not forget to close resources.



        public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
        Properties properties = new Properties();

        try (Reader reader = new BufferedReader(new FileReader(file))) {
        properties.load(reader);
        }

        consumer.accept(properties);

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
        properties.store(writer, "comment");
        }

        }




        Client code could look like this:



        updateProperties(
        new File("application.properties"),
        properties -> {
        properties.setProperty("two", "two_two_two");
        properties.setProperty("three", "three_three");
        });




        application.properties



        before update



        two=two_two
        one=one_one


        after update



        #comment
        #Fri Jan 04 18:59:12 MSK 2019
        two=two_two_two
        one=one_one
        three=three_three





        share|improve this answer


























          0












          0








          0







          This is pretty easy. First do read a property file to Properties. Then update it and save it afterwards. Do not forget to close resources.



          public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
          Properties properties = new Properties();

          try (Reader reader = new BufferedReader(new FileReader(file))) {
          properties.load(reader);
          }

          consumer.accept(properties);

          try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
          properties.store(writer, "comment");
          }

          }




          Client code could look like this:



          updateProperties(
          new File("application.properties"),
          properties -> {
          properties.setProperty("two", "two_two_two");
          properties.setProperty("three", "three_three");
          });




          application.properties



          before update



          two=two_two
          one=one_one


          after update



          #comment
          #Fri Jan 04 18:59:12 MSK 2019
          two=two_two_two
          one=one_one
          three=three_three





          share|improve this answer













          This is pretty easy. First do read a property file to Properties. Then update it and save it afterwards. Do not forget to close resources.



          public static void updateProperties(File file, Consumer<Properties> consumer) throws IOException {
          Properties properties = new Properties();

          try (Reader reader = new BufferedReader(new FileReader(file))) {
          properties.load(reader);
          }

          consumer.accept(properties);

          try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
          properties.store(writer, "comment");
          }

          }




          Client code could look like this:



          updateProperties(
          new File("application.properties"),
          properties -> {
          properties.setProperty("two", "two_two_two");
          properties.setProperty("three", "three_three");
          });




          application.properties



          before update



          two=two_two
          one=one_one


          after update



          #comment
          #Fri Jan 04 18:59:12 MSK 2019
          two=two_two_two
          one=one_one
          three=three_three






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 16:01









          oleg.cherednikoleg.cherednik

          7,21921219




          7,21921219

























              0














              Use Properties.store() to write the changed value back to the file:



              String PROPERTIES_PATH = "...";
              try {
              File f = new File(PROPERTIES_PATH);
              FileInputStream in = new FileInputStream(f);
              Properties config = new Properties();
              config.load(in);

              String epochFromTimeChanged= Long.toString(epoch_to2);
              config.setProperty("epochFromTime",epochFromTimeChanged);

              // get or create the file
              File f = new File(PROPERTIES_PATH);
              OutputStream out = new FileOutputStream(f);
              config.store(out, "My properties file comment");

              } catch (FileNotFoundException e1) {
              log.info("{} does not exist", PROPERTIES_PATH);
              } catch (IOException e) {
              log.error("Cannot access {}", PROPERTIES_PATH, e);
              }





              share|improve this answer




























                0














                Use Properties.store() to write the changed value back to the file:



                String PROPERTIES_PATH = "...";
                try {
                File f = new File(PROPERTIES_PATH);
                FileInputStream in = new FileInputStream(f);
                Properties config = new Properties();
                config.load(in);

                String epochFromTimeChanged= Long.toString(epoch_to2);
                config.setProperty("epochFromTime",epochFromTimeChanged);

                // get or create the file
                File f = new File(PROPERTIES_PATH);
                OutputStream out = new FileOutputStream(f);
                config.store(out, "My properties file comment");

                } catch (FileNotFoundException e1) {
                log.info("{} does not exist", PROPERTIES_PATH);
                } catch (IOException e) {
                log.error("Cannot access {}", PROPERTIES_PATH, e);
                }





                share|improve this answer


























                  0












                  0








                  0







                  Use Properties.store() to write the changed value back to the file:



                  String PROPERTIES_PATH = "...";
                  try {
                  File f = new File(PROPERTIES_PATH);
                  FileInputStream in = new FileInputStream(f);
                  Properties config = new Properties();
                  config.load(in);

                  String epochFromTimeChanged= Long.toString(epoch_to2);
                  config.setProperty("epochFromTime",epochFromTimeChanged);

                  // get or create the file
                  File f = new File(PROPERTIES_PATH);
                  OutputStream out = new FileOutputStream(f);
                  config.store(out, "My properties file comment");

                  } catch (FileNotFoundException e1) {
                  log.info("{} does not exist", PROPERTIES_PATH);
                  } catch (IOException e) {
                  log.error("Cannot access {}", PROPERTIES_PATH, e);
                  }





                  share|improve this answer













                  Use Properties.store() to write the changed value back to the file:



                  String PROPERTIES_PATH = "...";
                  try {
                  File f = new File(PROPERTIES_PATH);
                  FileInputStream in = new FileInputStream(f);
                  Properties config = new Properties();
                  config.load(in);

                  String epochFromTimeChanged= Long.toString(epoch_to2);
                  config.setProperty("epochFromTime",epochFromTimeChanged);

                  // get or create the file
                  File f = new File(PROPERTIES_PATH);
                  OutputStream out = new FileOutputStream(f);
                  config.store(out, "My properties file comment");

                  } catch (FileNotFoundException e1) {
                  log.info("{} does not exist", PROPERTIES_PATH);
                  } catch (IOException e) {
                  log.error("Cannot access {}", PROPERTIES_PATH, e);
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 16:04









                  Markus PscheidtMarkus Pscheidt

                  3,55033153




                  3,55033153






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042114%2fi-need-to-overwrite-the-value-in-property-file%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas