 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Riri Guest
|
Posted: Tue Jan 27, 2004 5:53 pm Post subject: / and |
|
|
I have a JOpenFile, when i open a file, for instance D:/Temp/Test.jpg,
method toString() gives D:TempTest.jpg.
How do i replace the by /?
replaceAll("", "/"); is not working
Thanks
R.
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Tue Jan 27, 2004 6:09 pm Post subject: Re: / and |
|
|
Riri wrote:
| Quote: | I have a JOpenFile, when i open a file, for instance D:/Temp/Test.jpg,
method toString() gives D:TempTest.jpg.
How do i replace the by /?
replaceAll("", "/"); is not working
|
This is a trap set by Sun for the unwary programmer. The replaceAll
method doesn't replace one String with another; it replaces a regular
expression with a replacement pattern. That means you have to escape at
least some special characters in both parameters. E.g.,
fileName = fileName.replaceAll("\\", "/");
(The second parameter doesn't need quite as much escaping as the first,
but you'll still need to escape backslash characters if they occur.)
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Tue Jan 27, 2004 10:53 pm Post subject: Re: / and |
|
|
"Chris Smith" ...
| Quote: | Riri wrote:
....
How do i replace the by /?
replaceAll("", "/"); is not working
...
fileName = fileName.replaceAll("\\", "/");
|
Would it not be better to use File.separator
as the second parameter, Chris?
That way when the (damn!) user goes and
wants to use it on the 'other' system it will
use the platform default separator.
--
Andrew Thompson
* http://www.PhySci.org/codes/ Web & IT help
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Tue Jan 27, 2004 11:35 pm Post subject: Re: / and |
|
|
Andrew Thompson wrote:
| Quote: | "Chris Smith" ...
| Riri wrote:
...
| > How do i replace the by /?
|
| > replaceAll("", "/"); is not working
..
| fileName = fileName.replaceAll("\\", "/");
Would it not be better to use File.separator
as the second parameter, Chris?
|
No! Actually, that's a very poor idea, because File.separator is a
backslash on some platforms, and replaceAll breaks when a backslash is
used as the replacement string. That was explained in my last email.
As a result, you'd have to do something like:
String sep = File.separator.replaceAll("\\", "\");
fileName = fileName.replaceAll("\\", sep);
But, that last bit may be a better idea *if* you're using the name in a
context where the platform file separator is used. On the other hand,
if you're building a JAR path (or anything else where '/' is used
consistently as the separator char), you want to use the literal slash.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Wed Jan 28, 2004 4:27 am Post subject: Re: / and |
|
|
Riri wrote:
| Quote: | I have a JOpenFile, when i open a file, for instance D:/Temp/Test.jpg,
method toString() gives D:TempTest.jpg.
How do i replace the by /?
|
If you have actual files involved, use java.io.File instead of strings.
Then use
File.getParentFile()
new File(File, String)
|
|
| Back to top |
|
 |
Boris Stumm Guest
|
Posted: Wed Jan 28, 2004 7:29 am Post subject: Re: / and |
|
|
Riri wrote:
| Quote: | I have a JOpenFile, when i open a file, for instance D:/Temp/Test.jpg,
method toString() gives D:TempTest.jpg.
How do i replace the by /?
replaceAll("", "/"); is not working
|
Try replace('', '/').
(Or, as mentioned already, replace('', File.separatorChar)
Boris Stumm
|
|
| Back to top |
|
 |
Riri Guest
|
Posted: Wed Jan 28, 2004 10:23 am Post subject: Re: / and |
|
|
Thank you but fileName = fileName.replaceAll("\\", "/"); doesn't work
either.
R
"Chris Smith" <cdsmith (AT) twu (DOT) net> a écrit dans le message de news:
[email]MPG.1a80573bbd5731cb9898ee (AT) news (DOT) pop4.net[/email]...
| Quote: | Riri wrote:
I have a JOpenFile, when i open a file, for instance D:/Temp/Test.jpg,
method toString() gives D:TempTest.jpg.
How do i replace the by /?
replaceAll("", "/"); is not working
This is a trap set by Sun for the unwary programmer. The replaceAll
method doesn't replace one String with another; it replaces a regular
expression with a replacement pattern. That means you have to escape at
least some special characters in both parameters. E.g.,
fileName = fileName.replaceAll("\\", "/");
(The second parameter doesn't need quite as much escaping as the first,
but you'll still need to escape backslash characters if they occur.)
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Wed Jan 28, 2004 2:18 pm Post subject: Re: / and |
|
|
Riri wrote:
| Quote: | Thank you but fileName = fileName.replaceAll("\\", "/"); doesn't work
either.
|
Then you need to define "doesn't work". Do you get an error message?
Is it compile-time or run-time? If you don't get an error message, then
what exactly was fileName equal to when you called that method, and what
is it equal to after calling the method?
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Riri Guest
|
Posted: Wed Jan 28, 2004 5:57 pm Post subject: Re: / and |
|
|
I have a jTextFiled that contains "D:TempPicture 023.jpg", i'd like to
open the image corresponding to that, but it souldn't be "", but "/", when
i try to do s.replaceAll(\\, "/");, i obtain the same thing
"D:TempPicture 023.jpg"
Thanks
Melina
|
|
| Back to top |
|
 |
Virgil Green Guest
|
Posted: Wed Jan 28, 2004 6:16 pm Post subject: Re: / and |
|
|
"Riri" <none (AT) nshotmail (DOT) com> wrote
| Quote: | I have a jTextFiled that contains "D:TempPicture 023.jpg", i'd like to
open the image corresponding to that, but it souldn't be "", but "/",
when
i try to do s.replaceAll(\\, "/");, i obtain the same thing
"D:TempPicture 023.jpg"
|
Show us the exact line of code you are using. Note that s.replaceAll()
returns a new string with the replacements. It does not change the original
String as String objects are immutable. Are you assigning the resulting
String (reference) to a variable?
- Virgil
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Sat Feb 07, 2004 7:39 am Post subject: Re: / and |
|
|
Riri wrote:
| Quote: | I have a jTextFiled that contains "D:TempPicture 023.jpg", i'd like to
open the image corresponding to that, but it souldn't be "", but "/"
|
Are you sure???
Last time I was checking things, that was the preferred path type on
Windows.
Then there are problems if you get to UNC paths.
|
|
| Back to top |
|
 |
Jon A. Cruz Guest
|
Posted: Sat Feb 07, 2004 7:40 am Post subject: Re: / and |
|
|
Boris Stumm wrote:
| Quote: |
Try replace('', '/').
(Or, as mentioned already, replace('', File.separatorChar)
|
Again, that can break paths on Windows.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|