Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file path Windows format to java format

Tags:

java

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.?

like image 927
Manoj Avatar asked Jun 17 '10 06:06

Manoj


People also ask

How do you get the path of the file in the file Java?

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.

What is the Java 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.


2 Answers

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

Find more details in the Docs

like image 174
Fred Avatar answered Sep 23 '22 15:09

Fred


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 

Strings 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.

like image 44
Martijn Courteaux Avatar answered Sep 20 '22 15:09

Martijn Courteaux