3

I have checked similar qestions where they suggested to use String as a parameter. But that is what I do.

My Code:

 uri.lastIndexOf(File.separator)

Where uri is: file:/C:/Users/oto@increase.dk/workspace/Maersk_Line_GaTracking/bin/

Maybe that is because Windows uses back slashes as file separator. But why then, when I retrieve the URI, I am getting forward slashes?

String uri = DataRetrieval.class.getProtectionDomain().getCodeSource().getLocation() + "";
Gaël J
  • 11,274
  • 4
  • 17
  • 32
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103

3 Answers3

3
CodeSource.getLocation() 

returns an URL object.

Separators in URLs are forward slashes.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
3

There's a difference between file names and URIs. File names use \ as a separator on Windows, but URIs always use /.

So if you're splitting file names, search for File.Separator, and if you're splitting URIs, search for '/'

OH, I should mention: / also works as a separator in most windows APIs, so for filenames, it's usually a good idea to map all the forward slashes to File.Separator before you start messing with it.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
1

URIs are defined to use / as their component separator.

martinez314
  • 12,162
  • 5
  • 36
  • 63