• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

URL, URI and local files

 
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today I tried java's URLConnection, trying to read a local Json-file  and to compare the lot with javascripts fetch(). I came up with

I noticed that new URL is deprecated, and that I should use URI.toURL:

Am I using the URL and the URI in a correct way? Is there a better way?

Thanks!
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This example  is the way I remember doing it.
Also, "localhost" is unnecessary.
 
Piet Souris
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Carey.

If I leave out 'localhost/', both with the URL and the URI, I get this error:

But I may be doing something wrong.

Edit: if I do not start with 'file://", then I do indeed not need the localhost. Complicated stuff...
 
Sheriff
Posts: 28323
95
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
The stack trace suggests you are trying to connect to an FTP server. In which case a file:// URL would be the wrong choice anyway.
 
Saloon Keeper
Posts: 15727
368
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:If I leave out 'localhost/', both with the URL and the URI, I get this error:


Do you mean:

Yeah, that wouldn't work. When you write it like that, it interprets "G" as a host name.

It's relatively easy to figure out how to construct an URL of any kind if you know a few simple rules. Most URLs are of one of the the following two variants:

Simply put, if you have two slashes after the scheme, then it means you will be specifying an authority. For the "file" scheme, the authority is really just the domain name of the system that you want to retrieve the file from, and file URLs don't have a query nor a fragment.

Here are two examples:

Now, the reason that the URL that Carey gave would also work, is because it's allowed to specify an empty authority, which for the "file" scheme means that the authority defaults to localhost. So the following URLs are all equivalent:
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To refine Stephan's response:

If you omit all leading slashes after "file:", that indicates a relative path location (on the local host). Relative paths should be used only with care.

The generally preferred syntax for a file resource path is "file:///directory/directory/file". In Windows, the top-level directory would be the drive ID, and if omitted assumes the currently logged drive.

UNC filename paths are valid in file URLs as well.

Finally, if memory serves, an alternative drive ID syntax allow using "|" following the drive ID, viz. "file:///G|/path/to/file". That can be used to eliminate confusion, since ":" has a specific meaning for URLs.

And, of course, masochists can use actual Windows file paths, allowing for the fact that the backslash character also has a specific meaning: "file:///G:\\path\\to\\file". But aside from the obvious hazards, that makes writing portable code harder.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:"file:///G|/path/to/file"


This is the one I'm familiar with..
 
Piet Souris
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Do you mean:


Yes,

Thank you, Stephan and Tim. When I started, I only knew URLs starting with 'https://', so I thought of using 'file://', as we say in Dutch 'not hindered by any relevant knowledge'.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
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
URLs can support all sorts of schemes. For example "gopher:" or "ftp:". Gopher was an old pre-google search engine, no longer supported.

However, to be useful, you'd feed the URL to some sort of consumer (for example, a web client). In which case, the consumer would typically inquire of a registry of schemes to see if it could process that scheme.  If the registry in question does not recognize the scheme, it will take a default action. For example, my FireFox browser will send the unparseable URL "fool://www.motley.com" out to its default search engine. The Linux "curl" command, on the other hand reports "curl: (1) Protocol "fool" not supported".

(Note: edited example URL because original was incorrectly pasted).
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic