Slash and Windows file paths


I quite often use properties files via the class java.util.Properties to obtain input for files and file path names. To ensure consistency, I’ve decided upon the forward slash for my Windows file paths, i.e.,


To ensure this is so, I use the String method replaceAll to convert any back slashes to a forward slash. So if a file path is written in one of my properties files with a back slash, I can convert it to the way I want it to be.


As mentioned, I use the String method replaceAll and a regular expression to do the conversion. On testing this idea, I used a text file with the following examples, all with a different path separator:




The following code shows that the above file paths can be converted with a forward slash.

 Path source = Paths.get("c:/junk", "gash.txt");
 String inrec = "";
 String newString = "";

try (BufferedReader buff = Files.newBufferedReader(source, Charset.forName("US-ASCII"))) {

inrec = buff.readLine();
 while (inrec != null) {

System.out.printf ("Original string = %s%n", inrec);
 newString = inrec.replaceAll("\\\\{1,}", "/");
 System.out.printf ("New string = %s%n%n", newString);

inrec = buff.readLine();

} catch (Exception e) {

Sample output

The following output can be seen from the above example file paths.

Original string = C:\Windows\sample\HitsumiAro\Olf\Mende\test.txt
New string = C:/Windows/sample/HitsumiAro/Olf/Mende/test.txt

Original string = C:\\Windows\\sample\\HitsumiAro\\Olf\\Mende\\test.txt
New string = C:/Windows/sample/HitsumiAro/Olf/Mende/test.txt

Original string = C:/Windows/sample/HitsumiAro/Olf/Mende/test.txt
New string = C:/Windows/sample/HitsumiAro/Olf/Mende/test.txt

See also
Java API documentation


This entry was posted in java and tagged , , . Bookmark the permalink.

2 Responses to Slash and Windows file paths

  1. It’s in point of fact a nice and helpful piece of information. I’m
    glad that you just shared this helpful information
    with us. Please stay us up to date like this. Thanks for

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s