37

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

Manoj
  • 5,707
  • 19
  • 56
  • 86

5 Answers5

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

Find more details in the Docs

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
Fred
  • 4,846
  • 1
  • 23
  • 21
14
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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
11

Java 7 and up supports the Path class (in java.nio package). You can use this class to convert a string-path to one that works for your current OS.

Using:

Paths.get("\\folder\\subfolder").toString()

on a Unix machine, will give you /folder/subfolder. Also works the other way around.

https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

dionoid
  • 500
  • 4
  • 9
3

Just check

in MacOS

File directory = new File("/Users/sivo03/eclipse-workspace/For4DC/AutomationReportBackup/"+dir);
File directoryApache = new File("/Users/sivo03/Automation/apache-tomcat-9.0.22/webapps/AutomationReport/"+dir); 

and same we use in windows

File directory = new File("C:\\Program Files (x86)\\Jenkins\\workspace\\BrokenLinkCheckerALL\\AutomationReportBackup\\"+dir);
File directoryApache = new File("C:\\Users\\Admin\\Downloads\\Automation\\apache-tomcat-9.0.26\\webapps\\AutomationReports\\"+dir);

use double backslash instead of single frontslash

so no need any converter tool just use find and replace

"C:\Documents and Settings\Manoj\Desktop" to "C:\\Documents and Settings\\Manoj\\Desktop"

-6
String path = "C:\\Documents and Settings\\someDir";
path = path.replaceAll("\\\\", "/");

In Windows you should use four backslash but not two.

Young Lee
  • 37
  • 1
  • 2
  • to form String that represents correct regular expression that matches backslash four backslashes are needed and example is true in the context of replaceAll method that expects regex as argument, but generally it is not the case – igor.beslic Apr 27 '18 at 11:50
  • Hm, why is this comment downvoted when the actual accepted answer is basically the same? – guenhter Mar 18 '19 at 16:27
  • 1
    @guenhter Maybe because it was posted more than 6 years after the accepted answer and adds nothing new as it is, as you points out, the same. Posting a copy of an existing answer adds no value and only creates unnecessary noise – Joakim Danielson Jun 24 '19 at 12:08
  • Makes sense. Thx – guenhter Jun 25 '19 at 07:56