• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 '\'???
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic