Java Magazine, July/August 2016
ORACLE COM JAVAMAGAZINE JULY AUGUST 2016 50 new to java 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 example 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 specify how symbolic links should be handled provided the underlying operating system 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 Hypothetical may not exist yet or may have been deleted It is therefore fundamentally different from a File In particular the system dependency Path is an abstract concept it isnt necessarily bound to any physical file path so you can talk easily about the locations of files that dont exist yet is manifested by Path being an interface not a class This enables different filesystem providers to each implement the Path interface and provide for system specific features while retaining the overall abstraction The elements of a Path consist of an optional root component which identifies the filesystem hierarchy that this instance belongs to Note that for example relative Path instances may not have a root component In addition to the root all Path instances have zero or more directory names and a name element The name element is the element farthest from the root of the directory hierarchy and represents the name of the file or directory The Path can be thought of consisting of the path elements joined together by a special separator or delimiter Path is an abstract concept it isnt necessarily bound to any physical file path This allows us to talk easily about the locations of files that dont exist yet Java ships with a Paths class that provides factory methods for creating Path instances Paths provides two get methods for creating Path objects The usual version takes a String and uses the default filesystem provider The URI version takes advantage of the ability of NIO 2 to plug in additional providers of bespoke filesystems This is an advanced usage and interested developers
You must have JavaScript enabled to view digital editions.