Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with different file.separator on Windows and UNIX in java

Tags:

java

file

I'm reviewing my code

My Java application runs on Windows and Unix using Java 6 and manipulates files. I know I can use the file.separator to get the file separator in a filesystem independent way but if I ever use specify windows file separator char directly because is the '\' which is also the Java escape character I can't do manipulation of the file path. So in my code I've always stored the filepath in unix notation by replacing \' with '/' ,and these paths are stored in a database so I thought there was an escaping problem there as well. So I was under the illusion that trying to use file.separator would also fail because it would return '\' rather than '\' but now realise only need \ if I actually explicity specify it myself in quotes.

Now I'm thinking this is all unnecessary and as long as I always use file.separator I don't need to do this conversion, am I right ?

EDIT: Found a case where it seems to be a problem

"C:\Fred\test1.txt".split("\\\\");
"C:\Fred\test1.txt".split(System.getProperty("file.separator"));

if I want to split the string by \ I have double it up because \ has special meaning in a regular expression, so the line using file.separator fails with

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
 ^
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.compile(Pattern.java:1466)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)

but on unix no such requirement the corresponding '/' should not be escaped

EDIT 2: This question has been asked before Splitting filenames using system file separator symbol the solution boils down to using Pattern.quote() around the input or trying to use file methods rather regular expressions. Both a little uneccessary I would prefer it if files could be viewed ins a system independent way, I not that Path in Java 7 has the same problem.

EDIT 3: Also seeing a problem with reading/writing from db, created a separate question on that Storing Windows Path in Database and retrieving with Hibernate using java

like image 926
Paul Taylor Avatar asked Jun 28 '12 09:06

Paul Taylor


People also ask

What is the alternative directory separator for Linux?

If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.

What is the file separator in Linux?

A file separator is a character that is used to separate directory names that make up a path to a particular location. This character is operating system specific.

Which is the valid separator in Java?

The most commonly used separator in java is a semicolon(;).


1 Answers

Yes, you are right - if you use File.Seperator you dont need any additional escaping because in Windows it is already escaped for you. From the java documentation.

separatorChar

public static final char separatorChar

The system-dependent default name-separator character. 
This field is initialized to contain the first character of the value of the system property file.separator. 
On UNIX systems the value of this field is '/';
on Microsoft Windows systems it is '\\'. 
like image 168
kjp Avatar answered Oct 17 '22 00:10

kjp