Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Differences between System.getProperty("file.seperator") and File.pathSeparator

 
Rajani Gummadi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

While I was working on some file operations in java, I came across the File.pathSeparator and found that this returns ";" and where as System.getParameter("file.separator") as "\". I'm using windows system.

Don't these two should denote the same ?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

No, the path separator and the file separator are not the same thing.

The path separator is used to separate elements on for example the classpath. On Windows, it's ";" and on Unix-like operating systems it's ":".

The file separator is used to separate directory names and filenames on a file path. On Windows, it's "\" and on Unix-like operating systems it's "/".

Note that class File has, besides a variable called pathSeparator, also a variable called separator. That last one is the same as System.getProperty("file.separator"). Also, there is also a system property called "path.separator", which is, ofcourse, the path separator.
 
Rajani Gummadi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper for clarifying and also for providing additional guidance.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On Windows, it's "\" and on Unix-like operating systems it's "/".

On at least the last three Windows versions, both \ and / are accepted. Not just from a Java program, but even in the Command Prompt window.
 
Paul Clapham
Sheriff
Posts: 21314
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:On at least the last three Windows versions, both \ and / are accepted. Not just from a Java program, but even in the Command Prompt window.

That's mostly true but there are a few exceptions. For example the command-line "ftp" program still requires \ as its path separator. However it doesn't handle file names containing the _ character properly so it can hardly be held up as a good example.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic