Results 1 to 11 of 11
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By SurfMan
  • 1 Post By JosAH

Thread: \\ or / or File.separator Clarification please!

  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default \\ or / or File.separator Clarification please!

    Is it really best practice to use File.separator when constructing a file path? Seems like a lot of extra keystrokes, though I can see the need for it if the program will be run on diffferent OSs. Still, it seems they'd have come up with something a bit easier, like letting the JRE swap file separators at runtime if needed.

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    I would say yes, another example is System.lineSeparator();

    BR,
    Kalin

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: \\ or / or File.separator Clarification please!

    The JVM accepts both path separators / and \; the compiler however uses the \ character as an escape character (that's why you have to type \\); so my opinion is to simpy use the / character, always.

    kind regards,

    Jos
    kalata likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    Disregard my comment, Jos is right.

  5. #5
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by kalata View Post
    I would say yes, another example is System.lineSeparator();

    BR,
    Kalin
    You gotta be joking! You write

    String text = "This is line " + lineNum + "." + System.lineSeparator() + "bla bla bla on the next line."

    instead of

    String text = "This is line " + lineNum + ".\nbla bla bla on the next line." ?

    Every class I'd write would have a class variable: String nLine = "System.lineSeparator()" ... which isn't so bad I guess.

  6. #6
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by JosAH View Post
    The JVM accepts both path separators / and \; the compiler however uses the \ character as an escape character (that's why you have to type \\); so my opinion is to simpy use the / character, always.

    kind regards,

    Jos
    Thanks Jos! Can you tell me what the JRE does when it encounters the comiled file separator? It probalby checks the system and uses the right one, but that would make all the talk about which to use and best practices kind of pointless...

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by NotTooOld View Post
    You gotta be joking! You write

    String text = "This is line " + lineNum + "." + System.lineSeparator() + "bla bla bla on the next line."

    instead of

    String text = "This is line " + lineNum + ".\nbla bla bla on the next line." ?

    Every class I'd write would have a class variable: String nLine = "System.lineSeparator()" ... which isn't so bad I guess.
    Yes, because Windows (especially Notepad) doesn't understand \n but only \r\n. (Legacy ftw!)

    {sarcasm on}
    Don't add that to every class. Just create the class below and save even more keystrokes:
    Java Code:
    public class S {
       public static final String N = "\n";
       public static final String NR = "\n\r";
    }
    You can now type: String text = "This is line " + lineNum + "." + S.N + "bla bla bla on the next line."
    {sarcasm off}
    kalata likes this.

  8. #8
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    I like it!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by NotTooOld View Post
    Thanks Jos! Can you tell me what the JRE does when it encounters the comiled file separator? It probalby checks the system and uses the right one, but that would make all the talk about which to use and best practices kind of pointless...
    This is what the API documentation for the File class has to say about it:

    The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character. The default name-separator character is defined by the system property file.separator, and is made available in the public static fields separator and separatorChar of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.
    kind regards,

    Jos
    NotTooOld likes this.
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by JosAH View Post
    This is what the API documentation for the File class has to say about it:



    kind regards,

    Jos
    That is real service! I really appreciate the extra mile you went on this.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default Re: \\ or / or File.separator Clarification please!

    Quote Originally Posted by NotTooOld View Post
    That is real service! I really appreciate the extra mile you went on this.
    Google for "Java API documentation" and bookmark the link, or (even better) download the entire documentation; you'll have everything you wanted and needed to know at your fingertips.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 11-30-2010, 05:04 PM
  2. JSplitPane separator customization
    By azzardo in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 01:31 PM
  3. Help with separator
    By eel in forum New To Java
    Replies: 13
    Last Post: 09-19-2010, 10:08 AM
  4. Remove separator between windows
    By Annie George in forum NetBeans
    Replies: 0
    Last Post: 09-09-2010, 09:04 AM
  5. Replies: 0
    Last Post: 04-04-2008, 02:48 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
  •