How to get rid of the starting slash in URI or URL?
Asked Answered
Q

5

10

I am using

URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = res.getPath();
String path2 = path.substring(1);

because the output of the method getPath() returns sth like this:

 /C:/Users/......

and I need this

 C:/Users....

I really need the below address because some external library refuses to work with the slash at the beginning or with file:/ at the beginning or anything else.

I tried pretty much all the methods in URL like toString() toExternalPath() etc. and done the same with URI and none of it returns it like I need it. (I totally don't understand, why it keeps the slash at the beginning).

It is okay to do it on my machine with just erasing the first char. But a friend tried to run it on linux and since the addresses are different there, it does not work...

What should with such problem?

Quarterphase answered 5/7, 2013 at 13:18 Comment(6)
Strip the first character if path contains ":", for Christ's sake...Engrail
For Christ's sake, if you read the first three lines, you could see, that I am doing it already, but it is not helpful since - why should I repeat myself, just read the post......Quarterphase
you are doing it unconditionally. Whether I 'm telling you to do it, only if ":" is presentEngrail
You can even go further and check for ":" especially in 3rd position.Engrail
well you did edit the comment, the : was not there before. Such answer however is alredy below.Quarterphase
Yes. And it's mine. Just tried to show it with code, if words are not enough.Engrail
E
4

As long as UNIX paths are not supposed to contain drive letters, you may try this:

URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = res.getPath();
char a_char = text.charAt(2);
if (a_char==':') path = path.substring(1);
Engrail answered 5/7, 2013 at 13:30 Comment(1)
thanks, I hope no other situation can occur with different OS.Quarterphase
H
11

Convert the URL to a URI and use that in the File constructor:

URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
File file = new File(res.toURI());
String fileName = file.getPath();
Hyperdulia answered 19/9, 2014 at 8:23 Comment(0)
E
4

As long as UNIX paths are not supposed to contain drive letters, you may try this:

URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = res.getPath();
char a_char = text.charAt(2);
if (a_char==':') path = path.substring(1);
Engrail answered 5/7, 2013 at 13:30 Comment(1)
thanks, I hope no other situation can occur with different OS.Quarterphase
R
2

Convert to a URI, then use Paths.get().

URL res = this.getClass().getClassLoader().getResource(dictionaryPath);
String path = Paths.get(res.toURI()).toString();
Refurbish answered 27/3, 2020 at 17:0 Comment(2)
Path.get may be deprecated in future, may be you could still use like this: String path = Path.of(res.toURI()).toString();Wallen
Btw, Path.of(...) is only available from Java11Wallen
R
0

You could probably just format the string once you get it.

something like this:

path2= path2[1:];

Rowlett answered 5/7, 2013 at 13:21 Comment(1)
Did you read the first three lines of this post? I am using it - I can not and do not want to use it since this attitude is not working on other than windows machine. + I believe there must exist a more elegant solution than this.Quarterphase
E
0

I was searching for one-line solution, so the best what i came up with was deleting it manually like this:

String url = this.getClass().getClassLoader().getResource(dictionaryPath).getPath().replaceFirst("/","");

In case if someone also needs to have it on different OS, you can make IF statement with

System.getProperty("os.name");
Enschede answered 14/4, 2022 at 11:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.