Sunteți pe pagina 1din 2

Chapter 11 - File NIO

NIO - Non blocking Input Output


Also called as New Input Output

Topics for Today:


- File System
- Path
- File
- File Operations like create, delete, check, copy, move, etc
- Listing the directories content
- FileVisitor

-----------------------------------------------------------
What is a File System?
- how your folders/directories and files are arranged in your computer
- arranged in a tree-like structure

Types of File System:


1. DOS - Disk Operating System
eg: Windows - where we have Drives
eg: Root directory is Drives
eg: C Drive, D drive

2. POSIX - Portable Operating System Interface


eg: Mac OS, Linux, Unix
eg: Root directory is \
eg: \home\abc\a.txt

----------------------------------------------------------
What is a Path?
eg:
C:\Program Files\Java\jdk1.8.0_20\bin
C:\Program Files\Java\jdk1.8.0_20\bin\javac.jar

A path is a location to a particular directory or a file

Types of Path:
1. Absolute Path
2. Relative Path

1. Absolute Path - starts from root node


eg:
C:\Program Files\Java\jdk1.8.0_20\bin\javac.jar
eg: E:\ajay\a.txt

2. Relative Path - does not start from root node


eg:
javac.jar
eg: a.txt
eg:
ajay\a.txt

----------------------------------------------------
java.nio.file package
Path Interface
Paths class

FileSystem
FileSystems

Path will create the path even if it does not exists. It will not create the path
in file system.
It is the work of Files class to check whether the file exists or not

---------------------------------------------------------
createFile() - will create a file
createDirectory() - will create a folder/directory
createDirectories() -will create all the non-existing directories

delete() - used to delete file/directory


deleteIfExists() - used to check before deleting a file/directory
but both will throw exception if you are trying to delete a folder/directory which
is not empty

----------------------------------------------------------

FileVisitor Interface
- predefined interface in java.nio.file package
- has 4 imp methods:
1. preVisitDirectory() - before visiting folder
2. postVisitDirectory() - after visiting folder
3. visitFile() - when it visits a file
4. visitFileFailed() - when visit file fails

-----------------------------------------------------------------------------
PathMatcher class

it wants to count the number of a particular files from the subdirectory also

if I use FileVisitor interface, I need to compulsory override all 4 methods of it.


But in case if i want selective methods to be overriden, then you need to use
SimpleFileVisitor class - is an implementation of FileVisitor

S-ar putea să vă placă și