9 Replies Latest reply: Jul 28, 2008 3:31 PM by 843785 RSS

    getProperty and file location

    843785
      Hello,

      I want to store a file's location in a text file that I can later reload. Right now I have something like :
      SortedMap<String,String> saveData = new TreeMap<String,String>();
      saveData.put("ppFILELOC", _programPropPanel.getOutputFileLocation());
      That writes it properly to the file so in the file it looks like :

      ppFILELOC=C:\Documents and Settings\Java\Desktop\SMt2-Jan29\craaaap.csv

      I use this to read it:
      Properties loadData = new Properties();
      loadData.load(new FileReader( fileName ));
      
      _programPropPanel.setOutputFileLocation(loadData.getProperty("ppFILELOC"));
      The problem is that when it reads it in, I get back :

      C:Documents and SettingsJavaDesktopSMt2-Jan29craaaap.csv

      I need it to keep the \'s. Anyone know how I could do this?
        • 1. Re: getProperty and file location
          843785
          I don't know what Properties isn't preserving the back slash, but I do know that classes like File are agnostic: you can use / or \ as a path separator.

          I whipped up a little demo and I see no problem with backslahes:
          import java.io.*;
          import java.util.*;
          
          public class TestProperties {
              static final String KEY = "mykey";
              static final String VALUE = "C:\\Documents and Settings\\Java\\Desktop\\SMt2-Jan29\\craaaap.csv";
          
              public static void main(String[] args) throws IOException {
                  File file = new File("test.properties");
                  store(file);
                  load(file);
              }
          
              static void store(File file) throws IOException {
                  Properties p = new Properties();
                  p.setProperty(KEY, VALUE);
                  OutputStream out = new FileOutputStream(file);
                  p.store(out, "here we go");
                  out.flush();
                  out.close();
              }
          
              static void load(File file) throws IOException {
                  Properties p = new Properties();
                  InputStream in = new FileInputStream(file);
                  p.load(in);
                  in.close();
                  String value = p.getProperty(KEY, "duh");
                  System.out.println("value from file = " + value);
                  System.out.println("value preserved = " + VALUE.equals(value));
              }
          }
          Post a self-contained example showing how they somehow don't work for you.
          • 2. Re: getProperty and file location
            DrClap
            Create a Properties object and add that entry to it. Then use Properties.save() to produce your properties file. If you don't do that then you have to modify your output to follow the escaping rules required for properties files. You could read about them in the API documentation for Properties, but you really don't want to be writing code that does that if you can't help it.
            • 3. Re: getProperty and file location
              843785
              Ah yes, both ways work. I didn't have the \\ because I just got the absolute path.
              • 4. Re: getProperty and file location
                843785
                Sch104 wrote:
                Ah yes, both ways work. I didn't have the \\ because I just got the absolute path.
                Huh? The double back slash is because back slash is the escape character in string literals. The string itself doesn't contain doubled back slashes:
                import java.io.*;
                import java.util.*;
                
                public class TestProperties {
                    static final String KEY = "mykey";
                
                    public static void main(String[] args) throws IOException {
                        File file = new File("test.properties");
                        store(file);
                        load(file);
                    }
                
                    static void store(File file) throws IOException {
                        Properties p = new Properties();
                        p.setProperty(KEY, file.getCanonicalPath());
                        OutputStream out = new FileOutputStream(file);
                        p.store(out, "here we go");
                        out.flush();
                        out.close();
                    }
                
                    static void load(File file) throws IOException {
                        Properties p = new Properties();
                        InputStream in = new FileInputStream(file);
                        p.load(in);
                        in.close();
                        String value = p.getProperty(KEY, "duh");
                        System.out.println("value from file = " + value);
                        System.out.println("value preserved = " + file.getCanonicalPath().equals(value));
                    }
                }
                • 5. Re: getProperty and file location
                  843785
                  [http://java.sun.com/javase/6/docs/api/java/util/Properties.html#store(java.io.Writer,%20java.lang.String)]

                  I see that Properties uses \ as an escape character in its formatting of the key and value strings, so be careful if you are trying to generate property files by hand.
                  • 6. Re: getProperty and file location
                    843785
                    I went with the other method of creating an instance of properties and setting the properties and then storing the properties file (equivalent to deprecated save() method).

                    This works great when there is a location for the output file. Sometimes, there will not be an output file if the user has not selected that option. Then when I try to save the properties I continue to get a null pointer exception due to nothing being there for the getOutputFileLocation.
                    public String getOutputFileLocation() {
                                return Starter._GUIParameters.outputFile.getAbsolutePath();
                        }
                    • 7. Re: getProperty and file location
                      843785
                      You need to think this through. What is the sound of saving properties to a non-existent location?
                      • 8. Re: getProperty and file location
                        843785
                        Oh sorry I was unclear.

                        The OutputFileLocation is one of the properties. Not the file where the properties are being saved.

                        So my property key would be "FILELOCATION" and the value would be the file's path. If that path is null, I get a null pointer exception, when in reality I don't care if that path is null or not. If it is null I don't need anything to be written for that property....

                        It is or is not writting depending on a checkbox so I suppose I could only write it if the checkbox was checked and not if it is blank.
                        • 9. Re: getProperty and file location
                          843785
                          Sch104 wrote:
                          So my property key would be "FILELOCATION" and the value would be the file's path. If that path is null, I get a null pointer exception, when in reality I don't care if that path is null or not. If it is null I don't need anything to be written for that property....
                          This just sounds like a programming bug.