Reading Java Properties file without escaping values

前端 未结 8 1681
滥情空心
滥情空心 2020-12-06 09:54

My application needs to use a .properties file for configuration. In the properties files, users are allow to specify paths.

Problem

Propert

相关标签:
8条回答
  • 2020-12-06 10:34

    Based on @Ian Harrigan, here is a complete solution to get Netbeans properties file (and other escaping properties file) right from and to ascii text-files :

    import java.io.BufferedReader;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.Reader;
    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Properties;
    
    /**
     * This class allows to handle Netbeans properties file. 
     * It is based on the work of  : http://stackoverflow.com/questions/6233532/reading-java-properties-file-without-escaping-values.
     * It overrides both load methods in order to load a netbeans property file, taking into account the \ that 
     * were escaped by java properties original load methods.
     * @author stephane
     */
    public class NetbeansProperties extends Properties {
        @Override
        public synchronized void load(Reader reader) throws IOException {
            BufferedReader bfr = new BufferedReader( reader );
            ByteArrayOutputStream out = new ByteArrayOutputStream();
    
            String readLine = null;
            while( (readLine = bfr.readLine()) != null ) {
                out.write(readLine.replace("\\","\\\\").getBytes());
                out.write("\n".getBytes());
            }//while
    
            InputStream is = new ByteArrayInputStream(out.toByteArray());
            super.load(is);
        }//met
    
        @Override
        public void load(InputStream is) throws IOException {
            load( new InputStreamReader( is ) );
        }//met
    
        @Override
        public void store(Writer writer, String comments) throws IOException {
            PrintWriter out = new PrintWriter( writer );
            if( comments != null ) {
                out.print( '#' );
                out.println( comments );
            }//if
            List<String> listOrderedKey = new ArrayList<String>();
            listOrderedKey.addAll( this.stringPropertyNames() );
            Collections.sort(listOrderedKey );
            for( String key : listOrderedKey ) {
                String newValue = this.getProperty(key);
                out.println( key+"="+newValue  );
           }//for
        }//met
    
        @Override
        public void store(OutputStream out, String comments) throws IOException {
            store( new OutputStreamWriter(out), comments );
        }//met
    }//class
    
    0 讨论(0)
  • 2020-12-06 10:36

    Why not simply extend the properties class to incorporate stripping of double forward slashes. A good feature of this will be that through the rest of your program you can still use the original Properties class.

    public class PropertiesEx extends Properties {
        public void load(FileInputStream fis) throws IOException {
            Scanner in = new Scanner(fis);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
    
            while(in.hasNext()) {
                out.write(in.nextLine().replace("\\","\\\\").getBytes());
                out.write("\n".getBytes());
            }
    
            InputStream is = new ByteArrayInputStream(out.toByteArray());
            super.load(is);
        }
    }
    

    Using the new class is a simple as:

    PropertiesEx p = new PropertiesEx();
    p.load(new FileInputStream("C:\\temp\\demo.properties"));
    p.list(System.out);
    

    The stripping code could also be improved upon but the general principle is there.

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