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
When to Use an Assertion and When to Use an Exception
Why Are Interface Method Invocations Slower Than Concrete Invocations
Factorial Using Recursion in Java
How Does Java Object Casting Work Behind the Scene
How Does Java Makes Use of Multiple Cores
Writing Custom Kafka Serializer
How to Pass C Structs Back and Forth to Java Code in Jni
Tomcat - Maxthreads VS Maxconnections
Is There a Method That Calculates a Factorial in Java
Constructor Synchronization in Java
Specific Difference Between Bufferedreader and Filereader
Why Does Intellij Give Me "Package Doesn't Exist" Error
How to Cast an Object to an Int
Sharing a Persistence Unit Across Components in a .Ear File
Parsing a Fixed-Width Formatted File in Java
Are There Any Java Method Ordering Conventions
How to Improve the Performance of G.Drawimage() Method for Resizing Images