0
private static final String BasePath = "/home/rafael/pesquisa/ImageSimilarity";

I am working on an Image comparison program and I have found this line of code from somewhere. However in the above case, the path is set for a linux system and as a Windows user, I want to set my path for windows device. So I changed the path to

private static final String BasePath ="C:\imgs"

However it seems an error and I guess it is with the forward slash and the backward slash. How do I set path string in my case? Any help will be appreciated.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
OBX
  • 6,044
  • 7
  • 33
  • 77
  • 6
    You can use the forward slash in windows aswell. Use `C:/imgs`. Or escape the backward slash by adding another backward slash. "\" is the escaping character for strings in Java. So `C:\\imgs` should do it, too. – kevcodez Jul 09 '15 at 10:30
  • will it yield the same result as C:\imgs ? – OBX Jul 09 '15 at 10:31
  • 2
    Naming Conventions: you should name a constant like BASE_PATH – Kuchi Jul 09 '15 at 10:34
  • The full explanation is [here](http://stackoverflow.com/a/39286358/4723795) – xenteros Sep 02 '16 at 14:39

4 Answers4

4

You need to escape the backslash in your windows path, like so:

private static final String BasePath = "C:\\imgs";

You can also just use a forwardslash:

private static final String BasePath = "C:/imgs";
Armand
  • 23,463
  • 20
  • 90
  • 119
1

You need to change

private static final String BasePath ="C:\imgs"

to

private static final String BasePath ="C:\\imgs"

The single \ is an escape character and is causing an issue "Invalid escape character", this is because \i is expected to be an escape literal. (Which, luckily, it is not.) \\ Is the valid escape literal for a single \, and thus it solves the problem.

Continuing with the problem scenario, If you had, for example

 String path = "c:\noproblem";

Then there is no issue compiling since \n is a valid escape character. (But it will cause other issues nonetheless.)

But what is even better, use the File.separator

From the Java Documentation

The default name-separator character is defined by the system property file.separator, and is made available in the public static fields separator and separatorChar of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.

Thus - it will change the use the seperator requiered by the Operating System.

Have a look at How to construct a file path in java

Reg
  • 10,717
  • 6
  • 37
  • 54
1

You can use File.separator and then you don't have to worry about platform specific path separators. e.g. below

private static final String BASE_PATH = "C:" + File.separator + "imgs";

More detail can be found here

Community
  • 1
  • 1
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
0

Your thing should work when you use

private static final String BasePath = "C:\\imgs";

Note:

Although you can still use forward slash for this to work, I would suggest you to use for File.separator in order to make it clear.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29