Results 1 to 5 of 5

Thread: Escape Sequence

  1. #1
    Punter is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Escape Sequence

    Hi,
    I want to add extra '\' to file path
    eg
    User selects directory like C:\ABC\XYZ say, through Dialog Box
    But I want to have the path as C:\\ABC\\XYZ

    so that I can use the file path as shown below


    new FileInputStream("C:\\ABC\\XYZ\\somefile")

    How do I add the extra '\' ??

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Why don't you use JFileChooser, directly?

  3. #3
    imaginationac is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    I'm curious as to why you want to use two backslashes like that, but to answer your question it's as simple as using the escape sequence twice in succession. Like so:
    Java Code:
    \\\\

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    First, Masijade made a *really* good suggestion.

    Second, you only escape characters in a String *literal*, for example, "This is a String literal."

    The escape character is backslash. If you want a backslash in your String literal, use two backslashes. "This is a literal with a \\." comes out

    This is a literal with a \.

    In your example, the user already entered the String, so there is no need to escape any characters; escape is for literals only.

    In addition, Java allows slashes for Windows file paths. Use slashes unless you absolutely have to use backslashes; file paths don't need them.
    Last edited by Steve11235; 02-10-2009 at 12:42 AM. Reason: fix incoherency

  5. #5
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Firstly.
    you can always do this manually
    Secondly.
    you may analyze the string by code to get any "\" and add another "\" to it )
    Thirdly.
    you can use JFileChooser which gets an absolute file path )

    ANd remember that Java can read or write files starting right from it's mother folder ) if you write code like a:
    new FileInputStream("MyApplicationFolder/MyFile.txt"); that won't be mistake )


    But maybe you don't want GUI for your program. Am I right?
    Last edited by Webuser; 02-10-2009 at 08:12 AM.

Similar Threads

  1. Escape Sequence \t does not display.
    By dreamingofgreen in forum New To Java
    Replies: 7
    Last Post: 07-22-2008, 05:50 AM
  2. Replies: 0
    Last Post: 02-14-2008, 11:00 AM
  3. Using escape sequence with like clause (%)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:57 AM
  4. Using escape sequence with like clause (_)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:55 AM
  5. Escape sequence problem
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-21-2008, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •