What's the difference between a Resource, URI, URL, Path and File in Java?
UPDATE 2017-04-12 Check JvR's answer as it contains more exhaustive and exact explanation!
Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:
File
represents a file or directory accessible via file system- resource is a generic term for a data object which can be loaded by the application
- usually resources are files distributed with the application / library and loaded via class-loading mechanism (when they reside on class-path)
URL#getPath
is getter on the path part of URL (protocol://host/path?query
)URL#getFile
as per JavaDoc returnspath+query
In Java, URI
is just a data structure for manipulating the generic identifier itself.
URL
on the other hand is really a resource locator and offers you features to actually read the resource via registered URLStreamHandler
s.
URLs can lead to file-system resources and you can construct URL for every file system resource by using file://
protocol (hence File
<-> URL
relation).
Also be aware that that URL#getFile
is unrelated to java.io.File
.
Why do I need File object; why isn't a Resource (URL) enough?
It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get File
from it. However not all resource URLs can be converted to File
s.
And is there a Resource object?
From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).
Which is better? newFile(File.separator) or Paths.get().toFile
I think they are equals. But the second one based on Paths is much simpler to read.
Paths is more recent API introduce with Java 7. So it can cause an issue for people using a very old version of Java.
java.io.File vs java.nio.Files which is the preferred in new code?
The documentation that you linked give the answer:
The java.nio.file package defines interfaces and classes for the Java
virtual machine to access files, file attributes, and file systems.
This API may be used to overcome many of the limitations of the
java.io.File class. The toPath method may be used to obtain a Path
that uses the abstract path represented by a File object to locate a
file. The resulting Path may be used with the Files class to provide
more efficient and extensive access to additional file operations,
file attributes, and I/O exceptions to help diagnose errors when an
operation on a file fails.
Check if a path represents a file or a folder
Assuming path
is your String
.
File file = new File(path);
boolean exists = file.exists(); // Check if the file exists
boolean isDirectory = file.isDirectory(); // Check if it's a directory
boolean isFile = file.isFile(); // Check if it's a regular file
See File
Javadoc
Or you can use the NIO class Files
and check things like this:
Path file = new File(path).toPath();
boolean exists = Files.exists(file); // Check if the file exists
boolean isDirectory = Files.isDirectory(file); // Check if it's a directory
boolean isFile = Files.isRegularFile(file); // Check if it's a regular file
Convert object from java.nio.file.Path to java.io.File
Both java.nio.file.Path
and java.io.File
classes provides a way to pass from the one to the other.
1) Invoking toFile()
on a Path
object returns a File
representing it.
Path.toFile()
javadoc :
Returns a
File
object representing this path. Where thisPath
is
associated with the default provider, then this method is equivalent
to returning aFile
object constructed with theString
representation
of this path.If this path was created by invoking the
File
toPath
method then there
is no guarantee that theFile
object returned by this method is equal
to the original File.
2) Reversely, invoking toPath()
on a File
object returns a Path
representing it.
File.toPath()
javadoc :
Returns a
java.nio.file.Path
object constructed from the this abstract
path. The resultingPath
is associated with the default-filesystem.The first invocation of this method works as if invoking it were
equivalent to evaluating the expression:
FileSystems.getDefault().getPath(this.getPath());
Subsequent invocations of this method return the same
Path
.
Related Topics
Uninitialized Object VS Object Initialized to Null
How to Attach Multiple Files to an Email Using Javamail
Reverse Java Graphics2D Scaled and Rotated Coordinates
Maximum Size of Hashset, Vector, Linkedlist
Jframe.Dispose() VS System.Exit()
Difference Between Wait and Blocked Thread States
A Confusion About Java String Literal Pool and String's Concatenation
Converting to Upper and Lower Case in Java
How to Loop User Input Until an Integer Is Inputted
What Is the Best Open-Source Java Charting Library? (Other Than Jfreechart)
How to Send List of Objects to View and Back to Post Method in Controller
Java 8 Streams - Collect VS Reduce
How to Add Days to a Date in Java