Java Reference
In-Depth Information
Files . copy ( source , target );
Files . move ( source , target );
// Utility methods to retrieve information
long size = Files . size ( target );
FileTime fTime = Files . getLastModifiedTime ( target );
System . out . println ( fTime . to ( TimeUnit . SECONDS ));
Map < String , ?> attrs = Files . readAttributes ( target , "*" );
System . out . println ( attrs );
// Methods to deal with file types
boolean isDir = Files . isDirectory ( target );
boolean isSym = Files . isSymbolicLink ( target );
// Methods to deal with reading and writing
List < String > lines = Files . readAllLines ( target , cs );
byte [] b = Files . readAllBytes ( target );
BufferedReader br = Files . newBufferedReader ( target , cs );
BufferedWriter bwr = Files . newBufferedWriter ( target , cs );
InputStream is = Files . newInputStream ( target );
OutputStream os = Files . newOutputStream ( target );
Some of the methods on Files provide the opportunity to pass optional arguments,
to provide additional (possibly implementation-specific) behavior for the operation.
Some of the API choices here produce occasionally annoying behavior. For exam‐
ple, by default, a copy operation will not overwrite an existing file, so we need to
specify this behavior as a copy option:
Files . copy ( Paths . get ( "input.txt" ), Paths . get ( "output.txt" ),
StandardCopyOption . REPLACE_EXISTING );
StandardCopyOption is an enum that implements an interface called CopyOption .
This is also implemented by LinkOption . So Files.copy() can take any number of
either LinkOption or StandardCopyOption arguments. LinkOption is used to spec‐
ify how symbolic links should be handled (provided the underlying OS supports
symlinks, of course).
Path
Path is a type that may be used to locate a file in a filesystem. It represents a path
that is:
• System dependent
• Hierarchical
• Composed of a sequence of path elements
Search WWH ::




Custom Search