• Post Reply Bookmark Topic Watch Topic
  • New Topic

Paths.get() removes slash  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gives http/abc:4532/sample. Removes one slash before abc.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The slash character, "/" is an escape character in Java; when you want one to show up you have to put 2--you must escape the escape character.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les, I'm afraid that's incorrect. The backslash \ is an escape character in Java string literals; the regular slash / is not and does not need to be escaped.

Paths.get(...) resolves a path on the file system. It does not expect an URL, but a filesystem path. So you get weird results if you give it an URL instead of a filesystem path.

Note that there is a version of Paths.get that takes an URI instead of a string. But you need to explicitly give it an URI object:

I doubt however how useful it is to call this with a http: URI. A Path is a path to something in a file system, so a file: URI would work, but you might not get anything that makes sense when you give it a http: URI. (You'll most likely get an exception because there's no filesystem provider for the "http" scheme).
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jasper,
Yes, you are absolutely right. I am afraid I have always had problems distinguishing the slash from the backslash... even when I say slash for some reason I am picturing a backslash in my mind.

Jesper de Jong wrote:Les, I'm afraid that's incorrect. The backslash \ is an escape character in Java string literals; the regular slash / is not and does not need to be escaped.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String path = Paths.get(URI.create("http://abc:4532/sample")).toString(); gives java.nio.FileSystemNotFoundException. Provider "http" not installed
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you actually using http://abc:4532/sample? That appears to be a non‑existent URI, so you are going to get an Exception.

If you have things on your own machine, I thought the URI started file:// but I may be mistaken.
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you actually using http://abc:4532/sample? That appears to be a non‑existent URI, so you are going to get an Exception.

If you have things on your own machine, I thought the URI started file:// but I may be mistaken.


To access local files via a URI provider, the URL is "file:///dir1/dir2/file". The first 2 slashes are part of the URL coding convention (One of these days, I really should read up on why, but if nothing else, they indicate that optional hostname follows). The third slash indicates the root of the filesystem. Omit this and you have a relative-path URI, not an absolute-path URI. For URIs, you should always use "real" slashes, not backslashes. Backslashes will sometimes be honored (when properly escaped), but they're awkward (need escapes) and not portable (only DOS/Windows uses them). A URI is a logical resource locator, not a literal OS filesystem path, so using forward slashes is a universal path notation. For example, on the old minicomputers I used to work with, a file path would be in the form "<volume>/dir1/dir2/file", but the URI would have been "file:///volume/dir1/dir2/file". Except that we didn't have URIs back then.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:String path = Paths.get(URI.create("http://abc:4532/sample")).toString(); gives java.nio.FileSystemNotFoundException. Provider "http" not installed

Without any additional libraries, you cannot use Path to access HTTP URLs. And I fail to see why you would need to. What is it you're trying to achieve in the first place? If you're trying to access content from the URL, URLConnection or a more high level library like Apache HttpClient will do just fine.
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to create new URI based on two String values - String path1 = "http://abc:4532"; String path2 = "sample";
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the java.net.URL and java.net.URI classes are what you should be using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!