-1

I would like to know how to make a relative file URL, for example:

assets/images/icon.png

When I try to enter that in as a file URL, it doesn't work.

What I want to do is use this is an environment such as:

new File("path/to/file")

The reason I am not giving code, is because I want to know how to do this under almost any circumstances.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
Zoe
  • 85
  • 9

2 Answers2

1

First, your code seems correct, the path will be relative. It may depend on your system if / is the correct separator, you may also need // instead of / but that is another story.


The easy and up-to-date answer: Don't use File at all.

The new I/O library of Java (NIO) is able to do all that for you, platform independent. It revolves around the classes Path, Paths and Files.

You create a path to your file by using:

Paths.get("path", "to", "file");

It will dynamically select the correct separators for your current system.

And you read it using methods from Files. For example a demo listing all contents of that file:

Path theFile = Paths.get("path", "to", "file");
Files.lines(theFile).forEach(System.out::println);

If you later need to use older methods which require a File object you can simply use the Path#toFile method (documentation).


Note that a relative path will always be relative to where your program has started. You can always check where you currently are by using:

System.out.println(System.getProperty("user.dir"));
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • `File` isn't quite as platform dependent as you suggest (since it's not actually tied to the real file-system), you can also do `System.out.println(new File(System.getProperty("user.home"), "abc.txt").getCanonicalPath());` – Elliott Frisch Oct 21 '17 at 22:18
  • I am aware of this but its good that you highlight it, thanks. – Zabuzard Oct 21 '17 at 22:26
0

I'm not sure if you are really asking about File objects or "file:" URLs. They are different things.

If you are talking about File objects, then the good news is that relative File objects are supported, and that new File("path/to/file") is valid. (Actually, it is valid irrespective of whether it resolves.)

If you are concerned about creating File objects in a way that works for all platforms, you can be assured that pathname syntax with forward slashes is understood by File ... even on a Windows machine.

For new code, you have the alternative of using java.nio.file.Path which has a richer API. But either File or Path can assemble a path programmatically from simple names, so you can often avoid dubious "string bashing" code to build paths,


If you are talking about "file:" URLs / URIs, the bad news is that relative "file:" URLs are not allowed. Specifically the W3C spec for the "file:" URI scheme requires the path of a "file:" URL/URI to have a root. For example:

 file:path/to/file

is not valid. For more details:

Now you can have a relative URI. But this is just a "path" and has to be resolved relative to some other URL.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216