1 / 12

File class in Java

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.

dahlen
Download Presentation

File class in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File class in Java

  2. 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

  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 = ...; BasicFileAttributesattr = 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 • Example: Charset charset = Charset.forName(“US-ASCII”); 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 InputStreamfor reading bytes from file • Example: InputStream in = Files.newInputStream(file); • newOutputStream(Path, Charset) • returns a OutputStream • Example: OutputStream out = Files.newOutputStream(file);

More Related