This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Paths.get() removes slash

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gives http/abc:4532/sample. Removes one slash before abc.
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String path = Paths.get(URI.create("http://abc:4532/sample")).toString(); gives java.nio.FileSystemNotFoundException. Provider "http" not installed
 
Marshal
Posts: 76860
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 26281
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Want to create new URI based on two String values - String path1 = "http://abc:4532"; String path2 = "sample";
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then the java.net.URL and java.net.URI classes are what you should be using.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic