File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

filename conversion

 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I've a small problem.
I want to retrieve a filename from the user, and then pass it to a FileInputStream object. The problem is that the user will ofcourse type in the file name as being 'c:\dirA\dirAA\file.ext' or something similar, whilst the object expects double '\\' as seperators:
new FileInputStream("c:\\dirA\\dirAA\\file.ext");
Now I can ofcourse write a loop to go through the code and change every single '\' into a double '\\', but I cant help feeling there must be an easier way.... anyone want to offer an idea?
TIA,
Maarten
[ February 05, 2004: Message edited by: Maarten Vergouwen ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor does not want double backslashes in the argument. It's just that to embed a single backslash in a Java string literal, you have to write a double one. The actual string represented in this Java code
new FileInputStream("c:\\dirA\\dirAA\\file.ext")
only contains the characters
"c:\dirA\dirAA\file.ext"
You can check this, if you don't believe me, by running
System.out.println("c:\\dirA\\dirAA\\file.ext".toCharArray().length)
and you'll find that the printed number corresponds to the length of the string with the single backslashes.
In any event, the problem you're imagining isn't a problem. Use the user's input as-is, and it will work just fine.
 
Nischal Topno
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ern said there is no need to escape the "\" character. The constructor does not need to escape the "\" character.
The following code will help you:

Even if the user enters c:\dirA\dirAA\file.ext (without two "\") in the console, the code is going to read the data from the file "file.ext"
[ February 06, 2004: Message edited by: Nischal Topno ]
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Billybob Marshall
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use forward slashes, regardless of the platform.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic