Good afternoon, there is a task, to read the settings when the program starts ... I do not understand how to implement it (java) ... here is the application:

public class MainClass { public static void main(String[] args) { Properties myProp = new Properties(); InputStream is = null; try { is = MainClass.class.getResourceAsStream("/my.properties"); myProp.load(is); FileWriter fileWriter = new FileWriter(myProp.getProperty("outPath")+"text_out.txt"); fileWriter.write(myProp.getProperty("text")); is.close(); fileWriter.close(); System.out.println("Fiel text_out.txt created"); Thread.sleep(5000); } catch (Exception e) { System.out.println(e.getLocalizedMessage()); } } } 

the essence is this, there is a certain file my.properties there are 2 lines in it, the first is the text, the second path to the file where this text should be placed:

 text = Привет! как дела! outPath = /Users/myMac/Downloads/rrr/ 

The structure of the project is as follows (going to maven):

 main java MainClass resources my.properties 

I create with the help of maven (package) executable jar file, I start everything works, but the same text is always displayed and the file is created in the same place ...

and moving the jar file anywhere, it always works ...

as far as I understand, my.properties is packed into a jar file, and from there infa is read ...

Attention is the question of how to write a program so that the program reads data from (suppose) a nearby file, and when the information in this file changes, does it react to these changes ???

    2 answers 2

    I did this:

     FileInputStream is = null; ... is = new FileInputStream("my.properties"); 

    made a jar-nickname, and just next to it put the file my.properties now everything works exactly as I need.

      Pass the path to my.properties as a parameter or try to read the file along a relative path, then it will be important what working folder is.

      To follow the changes, you can create a stream that will periodically check the date of modification of the file and reread it. Easier to make Timer and run periodically TimerTask .

      Update

      The second option, just specify the file name, then it will be taken from the current working folder:

       myProp.load(new FileInputStream("my.properties")); 
      • Is it really the only way to transfer as a parameter to a file with settings?