1

I have a property file and under that I have define a property called:

config.folder = C:\myfolder\configfolder

now the problem is that when loading properties, this property returns me the vale like this:

C:myfolderconfigfolder

I want to replace this single forward slash with back slash so it return me the correct directory path. I know this is not compliance with Java.String. If the user use double forward slash I am able to convert but how can I convert single slash.

Talha Bin Shakir
  • 2,563
  • 10
  • 38
  • 54

4 Answers4

3

A better approach is to change the slash from backslash to forward slash, like so:

config.folder = C:/myfolde/configfolder

Java knows how to interpret this structure.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I hope you meant the other way round - "from back slash to forward slash" unless I am reading your answer upside down :) – adarshr Jan 25 '12 at 09:09
  • 1
    @adarshr, I am usually confused with these slashes. I have rectified the statement, thanks!. lol – Buhake Sindi Jan 25 '12 at 09:27
2

Change it to: config.folder = C:\\myfolder\\configfolder

bchetty
  • 2,231
  • 1
  • 19
  • 26
1

I will suggest that you start using System Properties for this i.e. file.separator

String fileSeparator = System.getProperty("file.separator");

Now say you got the path as :

String str = "C:/myfolder/configfolder";
String fileSeparator = System.getProperty("file.separator");
str= str.replace("/", fileSeparator);
System.out.println(str);

OUTPUT is :

C:\myfolder\configfolder

This approach might help you implement your program in any OS For Example UNIX with "/" as the file separator for different components of the file path, and for WINDOWS with "\" as the file separator for components of the file path.

Hope this might help in some way.

Regards

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Although it works, doing it this way is not *really* necessary. – adarshr Jan 25 '12 at 09:08
  • @adarshr : Just watch my this edit, telling the benefit of using this approach for different Operating Systems, without hickups. Regards. – nIcE cOw Jan 25 '12 at 09:09
  • Agreed, I am trying to say that just having the property as `C:/myfolder/configfolder` will work in most Java applications as they will usually have the intelligence and flexibility to work with both `/` and `\ `. – adarshr Jan 25 '12 at 09:13
  • @adarshr : This approach is to nullify any unwanted exceptional cases that might can arise due to Operating System Differences. Since he wants to change it manually outside, so doing that with file.separator will give him/her the desired result without any headaches for future concerns. Regards – nIcE cOw Jan 25 '12 at 09:16
  • 1
    +1 It's true you can get away with forward slashes almost every time, but still this point is worth mentioning. Also see http://stackoverflow.com/questions/2417485/file-separator-vs-slash-in-paths – Joeri Hendrickx Jan 25 '12 at 09:50
  • @JoeriHendrickx : Too true, that post has a meaning. Regards – nIcE cOw Jan 25 '12 at 09:54
1

the best way to play with the file path literal is to use the system properties i.e.string file separator =System.getProperty ("file.separator") then you can replace it with ur slash to get the file path regards