2

I Am not able to read Properties File using Java.It Means In this Properties File Backward Slash is not working.It is showing like ,this destination :C:Usersxxx.a

String filename="D://Desktop//xxx.properties";
is = new FileInputStream(filename);
Properties prop=new Properties();
prop.load(is);
System.out.println("destination :"+prop.getProperty("destination"));

Property File is the :

destination=C:\Users\xxx.a\

Result is showing

destination :C:Usersxxx.a

But I want to show destination :C:\Usersxxx.a\

Can You Please suggest Me?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
md razi
  • 67
  • 4
  • 12

4 Answers4

3

\ is an Escape character.

forward slash / is used as path separator in Unix environment.
Back slash \ is used as path separator in Windows environment.
So, You need to use \\ or / as path separator. You can not directly use \ in java. Since, it is an escape character.

So,You need to make changes in your properties file to make your program work. Use either / or \\ as path separator in your properties file.

In your case you want to show as C:\Users\xxx.a\.
So, use C:\\Users\\xxx.a\\ in your properties file to get output as C:\Users\xxx.a\

Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51
  • No i dont want to add double slash ,if i add it will show in result as C:\Users\xxx.a\ .Is there any other way programatically? – md razi Oct 28 '14 at 10:42
0

You need to add two slashes to your properties file like this: destination=C:\\Users\\xxx.a\\

The other way is to swap the slashes in the properties file: destination=C:/Users/xxx.a/

A \ is an escape character so it is removed. Adding two slashes escapes the first so only one is left.

James Fox
  • 691
  • 7
  • 24
  • Thank For Reply...But I need to add only C:\Users\xxx.a\ If we add double slash It ll work... – md razi Oct 28 '14 at 10:08
  • If you add two slashes to your properties file it will only show one slash for your destination. With just one slash in the properties file it is getting escaped so nothing is showed. Having two slashes in the properties file means the first is escaped and the second will be used, hence only one appears. – James Fox Oct 28 '14 at 10:11
  • If we add 2 slash in properties ,it ll show in destinition single.yeah itz fine.but i dont want to add double slash in properties file – md razi Oct 28 '14 at 10:40
0

The \ character is used as an "escape character" in many programming languages. It gives a special meaning to the next character in the text. For example, \n encodes the special character "new-line".

Use \\ instead of \. This indicates to the parser that you mean the actual symbol, not an escape character. For example, your property value would be:

destination=C:\\Users\\xxx.a\\
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • I need to add only sing slash in Properties File.If we add double slash..it is working.. – md razi Oct 28 '14 at 10:17
  • @mdrazi The \\ is interpreted as a single backslash character in your actual property. Your final property value will contain only one `\' between each directory. – metacubed Oct 28 '14 at 10:21
0

You can store it in D:/Desktop/xxx.properties as

destination=C:/Users/xxx.a/

and show it with a single backslash

String fileName = prop.getProperty("destination");
System.out.println("destination: " + fileName); // shows: C:/Users/xxx.a/
System.out.println("destination: " + Paths.get(fileName)); // shows: C:\Users\xxx.a
SubOptimal
  • 22,518
  • 3
  • 53
  • 69