Presentation is loading. Please wait.

Presentation is loading. Please wait.

File class in Java.

Similar presentations


Presentation on theme: "File class in Java."— Presentation transcript:

1 File class 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 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

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

8 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

9 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

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

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

12 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 "File class in Java."

Similar presentations


Ads by Google