File Path Windows Format to Java Format

file path Windows format to java format

String path = "C:\\Documents and Settings\\Manoj\\Desktop";
path = path.replace("\\", "/");
// or
path = path.replaceAll("\\\\", "/");

Find more details in the Docs

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+)

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";

File path problems in windows environment

Replace

"%s/ramp_adapter/user_%d/ramp_file_receipt/%d"

with

"%s" + File.separatorChar + "ramp_adapter" + File.separatorChar + "user_%d" + File.separatorChar + "ramp_file_receipt" + File.separatorChar + "%d"

Replace

getAbsolutePath().replace("/.","")

with

getAbsolutePath().replace(File.separator + ".", "")

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

Apache Commons comes to the rescue (again). The Commons IO method FilenameUtils.separatorsToSystem(String path) will do what you want.

Needless to say, Apache Commons IO will do a lot more besides and is worth looking at.



Related Topics



Leave a reply



Submit