file class in java

17
IO Package in Java

Upload: menefer

Post on 05-Jan-2016

52 views

Category:

Documents


3 download

DESCRIPTION

File class 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: File class in Java

IO Package in Java

Page 2: File class 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

Page 3: File class in Java

Methods of the File class

Page 4: File class in Java

Methods of the File class (cont’d)

Page 5: File class in Java

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

Page 6: File class in Java

NIO Package in Java

Page 7: File class in Java

Core components

NIO has two pillars Channels

Buffers

Page 8: File class in Java

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

Page 9: File class in Java

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

Page 10: File class in Java

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

Page 11: File class in Java

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

Page 12: File class in Java

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

Page 13: File class in Java

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

Page 14: File class in Java

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

Page 15: File class in Java

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

Page 16: File class in Java

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

Page 17: File class in Java

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