This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

slash notation \ / ?  RSS feed

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering why when you write a file path in java the notation for slash is the reverse ie / rather than standard \
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether the notation is standard or not depends on the operating system.
For example, "\" is standard in Windows where as in Unix systems, "/" is the standard.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i see.

thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The backslash '\' is indeed standard on Windows, but it's quite inconvenient, because it's the escape character, and you must double it to make it appear properly in a String, i.e,

String path = "C:\\WINDOWS\\SYSTEM32";

Forward slashes don't need to be doubled, and it turns out that the Windows filesystem routines will accept them just fine!

But the really, truly correct way to describe paths in a Java program is to do it in a system-independent way using the static members "separatorChar" or "seperator", either of which contains the correct platform-dependent path separator character:

String path = theDirectory + File.separator + "filename.txt";
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!