Files

The File class is an abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent pathname strings to name files and directories. The conversion of a pathname string to or from an abstract pathname is inherently system-dependent.

The definition of absolute pathname also is system dependent. On UNIX systems, a pathname is absolute if its prefix is "/". On Microsoft Windows systems, a pathname is absolute if its prefix is a drive specifier followed by "\\", or if its prefix is "\\\\".

The canonical pathname is absolute pathname without redunant names such as "." and "..".

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 File class. Most useful in package are Path and Files.

You can use streams to read from or write to files.

// Read file located in a directory "mydir" relative 
// to the current working directory and is UTF-8 encoded.
Path path = FileSystems.getDefault()
    .getPath("mydir", "myfile.txt");

try(BufferedReader reader = Files.newBufferedReader(
               path, 
               StandardCharsets.UTF_8)){
...               
}

More examples

Methods

Path

A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. A root component, that identifies a file system hierarchy, may also be present.

This interface extends Watchable interface so that a directory located by a path can be registered with a WatchService and entries in the directory watched.

There is util class Paths, that allows create the Path objects from string and URI.

Methods
method description
getFileName() Returns the name of the file or directory denoted by this path as a Path object.
getName(ind) Returns a name element of this path as a Path object.
getNameCount() Returns the number of name elements in the path.
getParent() Returns the parent path, or null if this path does not have a parent.
getRoot() Returns the root component of this path as a Path object, or null if this path does not have a root component.
getFileSystem() Returns the file system that created this object.
startsWith(path) Tests if this path starts with the given path.
endsWith(path) Tests if this path ends with the given path.
isAbsolute() Tells whether or not this path is absolute.
iterator() Returns an iterator over the name elements of this path.
normalize() Returns a path that is this path with redundant name elements eliminated.
relativize(path) Constructs a relative path between this path and a given path.
resolve(strpath) Converts a given path string to a Path and resolves it against this Path.
resolve(path) Resolve the given path against this path.
resolveSibling(strpath) Converts a given path string to a Path and resolves it against this path's parent path.
resolveSibling(path) Resolves the given path against this path's parent path.
subpath(indBegin, indEnd) Returns a relative Path that is a subsequence of the name elements of this path.
register(watcher, events) Registers the file located by this path with a watch service.
register(watcher, events, modifiers) Registers the file located by this path with a watch service.
hashCode() Computes a hash code for this path.
equals(obj) Tests this path for equality with the given object.
compareTo(path) Compares two abstract paths lexicographically.
toAbsolutePath() Returns a Path object representing the absolute path of this path.
toRealPath(linkopt) Returns the real path of an existing file.
toFile() Returns a File object representing this path.
toString() Returns the string representation of this path.
toUri() Returns a URI to represent this path.