Presentation is loading. Please wait.

Presentation is loading. Please wait.

IO Package in Java.

Similar presentations


Presentation on theme: "IO Package in Java."— Presentation transcript:

1 IO Package in Java

2 File class Class File provides information about The class provides
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

3 Methods of the File class

4 Methods of the File class (cont’d)

5 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

6 NIO Package in Java

7 Core components NIO has two pillars Channels Buffers

8 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

9 Buffers Buffer is A Buffer is typically used as Types of buffers:
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

10 Properties of Buffers Buffers have 3 main properties clear method
Capacity Position Limit clear method position=0 limit = capacity compact method limit=capacity position= position of last unread data + 1

11 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

12 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”);

13 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

14 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

15 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());

16 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 BufferedWriter writer = Files.newBufferedWriter(file, charset);

17 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 OutputStream out = Files.newOutputStream(file);


Download ppt "IO Package in Java."

Similar presentations


Ads by Google