Reading/Writing to Properties Files inside the jar file

前端 未结 3 1706
庸人自扰
庸人自扰 2021-01-20 01:31

So i am getting back into writing Java after 4 years so please forgive any \"rookie\" mistakes.

I need to have a properties file where i can store some simple data f

相关标签:
3条回答
  • 2021-01-20 02:11

    Challenge: Read the Property file location in jar file Read the Property file Write the variable as system variables

    public static void loadJarCongFile(Class Utilclass )
        {
           try{         
                 String path= Utilclass.getResource("").getPath();
                 path=path.substring(6,path.length()-1);
                 path=path.split("!")[0];
                 System.out.println(path);
                 JarFile jarFile = new JarFile(path);
    
                   final Enumeration<JarEntry> entries = jarFile.entries();
                   while (entries.hasMoreElements()) {
                       final JarEntry entry = entries.nextElement();
                       if (entry.getName().contains(".properties")) {
                           System.out.println("Jar File Property File: " + entry.getName());
                           JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                           InputStream input = jarFile.getInputStream(fileEntry);
                           setSystemvariable(input);      
                           InputStreamReader isr = new InputStreamReader(input); 
                           BufferedReader reader = new BufferedReader(isr);
                           String line;
    
                           while ((line = reader.readLine()) != null) {
                               System.out.println("Jar file"+line);
                           }
                           reader.close();
                       }
                   }
           }
           catch (Exception e)
           {
              System.out.println("Jar file reading Error");
           }
        }
        public static void setSystemvariable(InputStream input)
        {
        Properties tmp1 = new Properties();
           try {
                 tmp1.load(input);
    
           for (Object element : tmp1.keySet()) {
                 System.setProperty(element.toString().trim(),
                               tmp1.getProperty(element.toString().trim()).trim());
                 }      
           } catch (IOException e) {
                 System.out.println("setSystemvariable method failure");
           }
        }
    
    0 讨论(0)
  • 2021-01-20 02:18

    You should not be trying to write to "files" that exist inside of the jar file. Actually, technically, jar files don't hold files but rather they hold "resources", and for practical purposes, they are read-only. If you need to read and write to a properties file, it should be outside of the jar.

    0 讨论(0)
  • 2021-01-20 02:29

    Your code writes to a local file mainProperties.properties the properties.

    After you run your part of code, there you will find that a file mainProperties.properties has been created locally.

    FileOutputStream fos = new FileOutputStream("mainProperties.properties");
    

    Could order not to confuse the two files you specify the local file to another name. e.g. mainAppProp.properties .

    • Read the complete contents of the resource mainProperties.properties.
    • Write all the necessary properties to the local file mainAppProp.properties.

     FileOutputStream fos = new FileOutputStream("mainAppProp.properties");
    

    switch if file exists to your local file , if not create the file mainAppProp.properties and write all properties to it.

    • Test if file mainAppProp.properties exists locally.
    • Read the properties into a new "probs" variable.
    • Use only this file from now on.

    Under no circumstances you can write the properties back into the .jar file.

    Test it like

        [...]
        if (propKey == null) {
        // Key is not present so enter the key into the properties file
        mainFile.setProperty(confirmKey, "testtest");
    
    
        [...]
        Reader reader = null;
        try
        {
        reader = new FileReader( "mainAppProp.properties" );
        Properties prop2 = new Properties();
        prop2.load( reader );
        prop2.list( System.out );
        }
        catch ( IOException e )
        {
        e.printStackTrace();
        }
        finally
        {
        if (reader != null) {
        reader.close(); 
        }
        }
        }
        [...]
       }
    

    output : with prop2.list( System.out );

    -- listing properties --
    defaultXMLPath2=testtest

    content of the file mainAppProp.properties

    #testtest3
    #Mon Jul 14 14:33:20 BRT 2014
    defaultXMLPath2=testtest

    0 讨论(0)
提交回复
热议问题