I need to convert the file path in windows say C:\Documents and Settings\Manoj\Desktop for java as C:/Documents and Settings/Manoj/Desktop .
Is there any utility to convert like this.?
In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.
An object that may be used to locate a file in a file system. It will typically represent a system dependent file path. A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter.
String path = "C:\\Documents and Settings\\Manoj\\Desktop"; path = path.replace("\\", "/"); // or path = path.replaceAll("\\\\", "/");
Find more details in the Docs
String path = "C:\\Documents and Settings\\Manoj\\Desktop"; String javaPath = path.replace("\\", "/"); // Create a new variable
or
path = path.replace("\\", "/"); // Just use the existing variable
String
s are immutable. Once they are created, you can't change them. This means replace
returns a new String where the target("\\"
) is replaced by the replacement("/"
). Simply calling replace
will not change path
.
The difference between replaceAll
and replace
is that replaceAll will search for a regex, replace doesn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With