08 mayo 2014

Read Properties con rutas de Windows

Cuando se leen rutas en formato Windows desde un properties de esta manera:

props.load(new FileInputStream("Ruta"));

Las rutas no las carga tal cual. Por ejemplo: param=C:\Temp aparecería como C:Temp dado que escapa todo \

Os sugiero este método manual para leer los valores de un properties TAL CUAL:
/**
 * Método para leer un fichero de properties literalmente. No escapa \
 * @param strFileName Ruta al fichero de properties
 * @return El objeto de properties completo con el contenido del fichero
 * @throws IOException En el caso de no encontrar el fichero de propiedades.
 */
private static Properties readPropertiesFile(String strFileName) throws IOException {  
DataInputStream dis = new DataInputStream(new FileInputStream(strFileName));  
BufferedReader br = new BufferedReader(new InputStreamReader(dis));  
Properties properties = new Properties();
        try {  
            String eachLine = null;  
            String[] temp = null;  
            while((eachLine=br.readLine())!=null) {  
                temp = eachLine.split("=");  
                if(temp.length>1) {  
                    properties.put(temp[0].trim(), temp[1].trim());  
                }  
            } 
            return properties;
        } catch (FileNotFoundException e) {  
            throw e;  
        } catch (IOException e) {  
        throw e;  
        } finally{
        dis.close();
        br.close();
        }
  }