aspose file tools*
The moose likes JSP and the fly likes How to replace '\' character by '/' one???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to replace Watch "How to replace New topic
Author

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

Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
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

Joined: Oct 27, 2005
Posts: 19672
    
  18

String.replace should help you; you have to escape the \ so it would be


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
Posts: 135
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: May 05, 2008
Posts: 72

Alternatively, you ca use "File.separator", that way, your code becomes protable automatically, here's an example to do it:


When the compiler's not happy, ain't nobody happy.
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

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

Joined: Jan 22, 2008
Posts: 135

. . . 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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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

Joined: Jun 15, 2008
Posts: 11
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

Joined: Jan 22, 2008
Posts: 135
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace '\' character by '/' one????