input/output fundamentals (contd.) - wordpress.com · input/output fundamentals (contd.) nio...
TRANSCRIPT
Input/Output
Fundamentals
(Contd.)
NIO Package
Java NIO package provide one more utility APIs for manipulating files and directories.
Java Files Class
Java Files class contains static methods that work on files and directories.
This class is used for basic file operations like create, read, write, copy and delete the files or directories of the file system.
Java Paths & Paths
Path: This is the interface that replaces java.io.File class as the representation of a file or a directory when we work in Java NIO.
Paths: This class contains a static method to create Path instance.
Javav IO vs NIO Package
How to create Path
We can create an object of Path by calling Paths.get(String first, String... more) method of Paths class.
Path path1 = Paths.get("/tmp/file.txt"); // For UNIX
Path path2 = Paths.get("D:/data/file.txt"); // For Windows
We can also create an object of Path by separating parts of the path in Paths.get()
method.
Path path1 = Paths.get("/tmp", "file.txt");
Path path2 = Paths.get("D:", "data", "file.txt");
Path path3 = Paths.get("D:/data", "file.txt") ;
Create a file using Files
Files class provides createFile(Path filePath, FileAttribute<?>… attrs) method
to create file using specified Path.
Create Directories
Files class provides createDirectory(Path dir, FileAttribute<?>… attrs) and
createDirectories(Path dir, FileAttribute<?>… attrs) methods to create single and multi
level directories using specified Path.
Read file data using Files Class
Files class provides following
methods for reading file.
readAllBytes(Path path): This
method reads all the bytes from
the file at given path and returns
the byte array containing the
bytes read from the file.
readAllLines(Path
path,Charsetcs): This method read all lines from the file at
given path and returns the List
containing the lines from the file.
Copy Files
Files class provide copy(Path source, Path target, CopyOption… options)
methodthat copies given source file to specified target file and it returns path
of target file.
Move FilesJava Files class prov ides move(Path source,
Path target, CopyOption… options) method that move or rename a source file to target file and returns the path of target file. Option
parameter may include following:
REPLACE_EXISTING: It means if the target file exists then replaces it if it is not a non-empty directory.
ATOMIC_MOVE: It means move is performed as
atomic file system operation and all other options are ignored.
This method throws FileAleadyExistsException if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not
specified.
This method throws DirectoryNotEmptyExceptionif REPlACE_EXISTING option is specified but the file cannot be replaced because it is a non-
empty directory.
Write Files
Java NIO Files class provides write(Path path, byte[]
bytes, OpenOption… options) method that writes bytes
to a file at specified path.
The options parameter specifies how the file is created
or opened. If no option is specified then it consider
CREATE, TRUNCATE_EXISTING and WRITE options by
default. This means it opens the file for writing and creates if the file does not exist or truncate existing file
to size of 0 if it exists.
All the bytes in byte array are written to the file. This
method ensures that the file is closed when all the bytes
have been written and returns the path of written file.
Example Code
Walk File Tree
Files class provides the
following method:
walkFileTree(Path start,
FileVisitor<? Super Path> visitor)
It is used to traverse the
directory.
It traverses the directory at
specified path recursively and
returns the starting file.
Thank You!