Java Reference
In-Depth Information
Discussion
The Path object performs many of the same functions as the original File object. For
simplest cases, its usage is similar, except you get an instance from the Paths factory class
instead of by direct instantiation:
Path p = Paths . getPath ( / home / ian /. profile );
iif (! p . exists ()) {
// some warning here
} else
else {
// use p.size() etc.
}
One way that Path goes beyond the original is in the ability (by calling its register()
method) to set up a Watcher Service (see Using the FileWatcher Service to Get Notified
about File Changes ) to get notified of changes to a filesystem entry (such as new files created
in a directory).
The Files class contains an array of methods for dealing with file paths, including:
▪ Copying
▪ Moving
▪ Deleting
The PathsFilesDemo program shown here illustrates some of these operations:
Path p = Paths . get ( "my_junk_file" );
boolean
boolean deleted = Files . deleteIfExists ( p );
InputStream is =
PathsFilesDemo . class . getResourceAsStream ( "/demo.txt" );
long
long newFileSize = Files . copy ( is , p );
System . out . println ( newFileSize );
final
final Path realPath = p . toRealPath ();
System . out . println ( realPath );
realPath . forEach ( pc -> System . out . println ( pc ));
Files . delete ( p );
Create an abstract Path
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search