input/output fundamentals (contd.) - wordpress.com · input/output fundamentals (contd.) nio...

13
Input/Output Fundamentals (Contd.)

Upload: others

Post on 22-May-2020

52 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

Input/Output

Fundamentals

(Contd.)

Page 2: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 3: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

Javav IO vs NIO Package

Page 4: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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") ;

Page 5: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

Create a file using Files

Files class provides createFile(Path filePath, FileAttribute<?>… attrs) method

to create file using specified Path.

Page 6: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 7: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 8: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 9: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 10: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 11: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

Example Code

Page 12: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

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.

Page 13: Input/Output Fundamentals (Contd.) - WordPress.com · Input/Output Fundamentals (Contd.) NIO Package Java NIO package provide one more utility APIs for manipulating files and directories

Thank You!