IO Package in Java
Jan 05, 2016
IO Package in Java
File class
Class File provides information about Files and directories
The class provides A constructor with a String argument
Specifying the name of a file or directory
The name can be either an absolute path or a relative path
Methods of the File class
Methods of the File class (cont’d)
FileFilter interface
FileFilter Used for filtering the set of files shown to the user
Method boolean accept(File pathName)
Tests whether or not the file is acceptable
Used in conjunction with File[] listFiles method of the File class
To list files satisfying a given filter
NIO Package in Java
Core components
NIO has two pillars Channels
Buffers
Channels
Channel is similar to a stream but Is bidirectional unlike a stream
And always read from and write to a buffer
Some channel implementations in Java NIO include FileChannel DatagramChannel SocketChannel ServerSocketChannel
Buffers
Buffer is A container that can hold a finite number of elements
A Buffer is typically used as Write data to the buffer Call the flip method Read data out of the buffer Either call the clear method or the compact method
Types of buffers: ByteBuffer, CharBuffer, DoubleBuffer FloatBuffer, IntBuffer, LongBuffer
Properties of Buffers
Buffers have 3 main properties Capacity Position Limit
clear method position=0 limit =
capacity compact method
limit=capacity position=
position of last unread data + 1
NIO package
File information can also be retrieved using Classes of the sub-packages of the java.nio package
In particular, the following classes can be used: Path
Objects represent the location of a file or directory Paths
provides static methods used to create a Path object Files
provides static methods for common file and directory manipulations DirectoryStream
enables a program to list the content of a directory
Path class
Path is a programmatic representation of a path in file system
used to examine, locate, and manipulate files
composed of methods that can among other things obtain information about path through getFileName()
compare two paths using the equals() method
instantiated by means of the get method of the Paths class Example: Path p = Paths.get(“sample.txt”);
Files class
Files provides support for common file and directory manipulations
exists(Path) Verifies existence of a file or a directory
isReadable(Path), isWritable(Path), isExecutable(Path) Checks file accessibility
isSameFile(Path, Path) Checks whether two paths locate the same file
delete(Path) and deleteIfExists(Path) Delete files or directories
copy(Path, Path)and move(Path, Path) Copy and move files or directories
Files class (cont’d)
Files allows for management of meta-data
size(Path) size of specified file in bytes
isDirectory(Path) returns true if specified path is a directory
getLastModifiedTime(Path) File’s last modified time
getOwner(Path) Owner of specified file
readAttributes(Path, Class) Reads attributes of a file in one bulk operation
Basic File Attributes (Example)
Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
System.out.println("isDirectory: " + attr.isDirectory());
System.out.println("size: " + attr.size());
Reading, writing and creating files using Files class
Buffered I/O methods for text files in Files class newBufferedReader(Path, Charset)
opens a file for reading returning a BufferedReader Example: Charset charset = Charset.forName(“US-ASCII”);
BufferedReader reader = Files.newBufferedReader(file, charset);
newBufferedWriter(Path, Charset) returns a BufferedWriter Example: Charset charset = Charset.forName(“US-ASCII”);
BufferedWriter writer = Files.newBufferedWriter(file, charset);
Reading, writing and creating files using Files class (cont’d)
Unbuffered I/O methods for text files in Files class newInputStream(Path, OpenOption...)
returns an InputStream for reading bytes from file Example: InputStream in = Files.newInputStream(file);
newOutputStream(Path, Charset) returns a OutputStream Example: OutputStream out = Files.newOutputStream(file);