How to Store Network Path in Properties File? Backslash Weirdness.

S

Skinny Guy

How can I store and correctly read back a network path in a Properties
file?

\\servername\\sharename\directory\

I tried different combinations of backslashes, then tried to escape
the last backslash (because its the Properties continuation char), but
none of my combinations worked after reading the property back into a
string.

I want to read it back into a String so that the String will contain
the exact characters above. How should the property look in the
Properties file?
 
C

Carl

Skinny said:
How can I store and correctly read back a network path in a Properties
file?

\\servername\\sharename\directory\

I tried different combinations of backslashes, then tried to escape
the last backslash (because its the Properties continuation char), but
none of my combinations worked after reading the property back into a
string.

I want to read it back into a String so that the String will contain
the exact characters above. How should the property look in the
Properties file?

This works for me by escaping the backslash chars. "\" becomes "\\", and
"\\" becomes "\\\\".

In the props file, it should appear as:
netPath=\\\\server\\share\\directory\\

example:

Properties props = new Properties();
Properties propsIn = new Properties();

String pathString = new String("\\\\server\\share\\directory\\");

props.put("netPath", pathString);

try{
File file = new java.io.File(System.getProperty("user.home"),
"test.file");

if(!file.exists()){
file.createNewFile();
}

FileOutputStream fos = new FileOutputStream(file);
props.save(fos, "test");
propsIn.load(new FileInputStream(file));

} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}

System.out.println("Read: " + propsIn.get("netPath"));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top