Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to replace '\' character by '/' one????

 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!!
I am getting an usual problem during converting windows-style path name to Linux-style path name.

Suppose i have path name of file 'System.txt' as
"C:\Documents and Settings\prabhat_kumar04\Desktop\Prabhat\System.txt" ;

i want it as
"C:/Documents and Settings/prabhat_kumar04/Desktop/Prabhat/System.txt" ;

Any suggestions how do we replace '/' character by '\'???
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.replace should help you; you have to escape the \ so it would be
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't help as i have tried following code


At compile time i am getting error saying
C:\Documents and Settings\prabhat_kumar04\My Documents\JAva\test3.java:5: illegal escape character
String name="C:\Documents and Settings\prabhat_kumar04\Desktop\Prabhat\System.txt" ;

here it is treating '\D' as illegal escape character .
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you have \D in your String literal. You shouldn't put \ in a String literal, but \\. Try escaping all the \s.

. . . and I shall delete your unintentional duplicate posting.
[ June 18, 2008: Message edited by: Campbell Ritchie ]
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, you ca use "File.separator", that way, your code becomes protable automatically, here's an example to do it:
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, there are some syntax errors in that code, but i hope you get my point, the File.Separator macro gets replaced with the file separating character that is native to the running OS, so you dont have to worry about conversion..
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

. . . and I shall delete your unintentional duplicate posting.

As i per my knowledge this is not DUPLICATE posting.

I know that the string that i am using it not valid one . But,the problem is when your are browsing some file in Windows OS , you will get that INVALID java string. Before processing i have to convert it as "C:\\Documents and Settings\\prabhat_kumar04\\Desktop\\Prabhat\\System.txt" or
"C:/Documents and Settings/prabhat_kumar04/Desktop/Prabhat/System.txt"

But the problem remains same that you are getting the inavlid String as input. So, what should i do now??
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion, but you actually had two copies of the same post in this thread; I got rid of one.
 
Paul Sisco
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prabhat Gupta:

As i per my knowledge this is not DUPLICATE posting.

I know that the string that i am using it not valid one . But,the problem is when your are browsing some file in Windows OS , you will get that INVALID java string. Before processing i have to convert it as "C:\\Documents and Settings\\prabhat_kumar04\\Desktop\\Prabhat\\System.txt" or
"C:/Documents and Settings/prabhat_kumar04/Desktop/Prabhat/System.txt"

But the problem remains same that you are getting the inavlid String as input. So, what should i do now??



When you use getPath on the File Object, you should not get an error related to the escape character. You only need to escape the "\" character when you are using it in a string literal defined in the code.



This is the output:
c:\out\Test.txt
c:/out/Test.txt

Can you post some of the code to show how you are getting the filename?


Paul
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Paul ,
I am describing the exact problem.I have used the struts-framework to develop some application.In the JSP i have a browse button and user browse some file through it.since In windows OS the path separator is '\'.
I have to store this value into some FORM variable that i have declared as string type .
And i have to use this information to read the file that can be accessed only by that path-name.

I have two choice. First, before sending it to FORM use javascript to change it into appropriate format .Second is do some String operation on this after getting it into FORM variable.

Suggestions are very welcomed

Thanks
Prabhat
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prabhat Gupta:
Ok Paul ,
I am describing the exact problem.
Why didn't you do that earlier? Tell The Details

I think this is no longer a beginner's question, and it would sit better on the JSP forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic