Java File Path Windows/Linux

File path names for Windows and Linux

Normally, when specifying file paths on Windows, you would use backslashes. However, in Java, and many other places outside the Windows world, backslashes are the escape character, so you have to double them up. In Java, Windows paths often look like this: String WinDir = "C:\\trash\\blah\\blah";. Forward slashes, on the other hand, do not need to be doubled up and work on both Windows and Unix. There is no harm in having double forward slashes. They do nothing to the path and just take up space (// is equivalent to /./). It looks like someone just did a relpace of all backslashes into forward slashes. You can remove them. In Java, there is a field called File.separator (a String) and File.separatorChar (a char), that provide you with the correct separator (/ or \), depending on your platform. It may be better to use that in some cases: String WinDir = "C:" + File.separator + "trash" + File.separator + "blah" + File.separator + "blah";

Is there a way to check file paths in Java for Windows paths specifically?

It took me a while to understand what you’re asking for. You want to check whether a path is a valid absolute path for Windows, even when running on non-Windows systems.

I don’t think there is any way to access to the FileSystem provider for Windows when running on non-Windows systems. So I would just make do with a regular expression:

boolean validWindowsAbsolutePath =
path.matches(
"([a-zA-Z]:|\\\\)" +
"(\\\\[^<>:\"/\\\\|?*\u0000-\u001f]+)+")
&&
!path.matches(
"(|.*\\\\)(?i:con|prn|aux|nul|com[1-9]|lpt[1-9])" +
"(\\.[^.]*)?");

Reference: Naming Files, Paths, and Namespaces

File path issue on windows vs Unix in java

Use FileSystem.getSeparator() or System.getProperty("file.separator") instead of using slashes.

EDIT:
You can get an instance of FileSystem via FileSystems.getDefault (JDK 1.7+)

Path.equals behaves different on Windows and Linux

Path separator is different for Windows and Linux.

For Windows is \

For Linux is /

Safe way in java of building paths that work on both evironments is

Path filePath = Paths.get("path1", "path2");

In your case you use a String to form a Path. So in Windows

 String path2 = "path1/file1.jpg";
Paths.get(path2) -> results in "path1\file1.jpg"

It converts the separator / to a windows separator \

After that conversion both path1 and path2 are the same

Now when you run in Linux the following code

      String path1 = "path1\\file1.jpg"; -> this will not be ok for linux and also will not try to convert it to / as the first \ is an escape character
String path2 = "path1/file1.jpg"; -> this will be ok for linux /
System.out.println(Paths.get(path1));
System.out.println(Paths.get(path2));
System.out.println(Paths.get(path1).equals(Paths.get(path2)));

Java Paths in Windows for Linux environment

Check this post: Is there a Java utility which will convert a String path to use the correct File separator char?

Basically FilenameUtils.separatorsToSystem(String path), of Apache Commons, will help achieve what you are trying to do.

If you do not want to import the entire dependency this is what the method does:

String separatorsToSystem(String res) {
if (res==null) return null;
if (File.separatorChar=='\\') {
// From Windows to Linux/Mac
return res.replace('/', File.separatorChar);
} else {
// From Linux/Mac to Windows
return res.replace('\\', File.separatorChar);
}
}

UPDATE:
As @Slaw said, this solution is still Platform-dependent. You could modify the method to take an extra parameter, and decide if you want the output string in "Unix" or "Windows", something like this:

String changeFileSeparators(String res, boolean toUnix) {
if (res==null) return null;
if (toUnix) {
// From Windows to Linux/Mac
return res.replace('\\', '/');
} else {
// From Linux/Mac to Windows
return res.replace('/', '\\');
}
}

How to specify the path to access a file in both windows and linux uniquely using slash seperator

This works fine when i used file.separator.

public static final String QWD = File.separator +"qark-master" + File.separator +"qark" + File.separator +"qarkMain.py";
public static final String MANIFESTPATH=File.separator +"apktool"+ File.separator +"AndroidManifest.xml";

Java file path in Linux

Looks like you are missing a leading slash. Perhaps try:

Scanner s = new Scanner(new File("/home/me/java/ex.txt"));

(as to where it looks for files by default, it is where the JVM is run from for relative paths like the one you have in your question)



Related Topics



Leave a reply



Submit