File class in Java File class l Class
- Slides: 12
File class in Java
File class l Class File provides information about l l Files and directories The class provides l A constructor with a String argument l Specifying the name of a file or directory l The name can be either an absolute path or a relative path
Methods of the File class
Methods of the File class (cont’d)
File. Filter interface l File. Filter l Used for filtering the set of files shown to the user l Method l boolean accept(File path. Name) § l Tests whether or not the file is acceptable Used in conjunction with l File[] list. Files method of the File class § To list files satisfying a given filter
NIO package l File information can also be retrieved using l l Classes of the sub-packages of the java. nio package In particular, the following classes can be used: l Path l l Paths l l provides static methods used to create a Path object Files l l Objects represent the location of a file or directory provides static methods for common file and directory manipulations Directory. Stream l enables a program to list the content of a directory
Path class l Path is l a programmatic representation of a path in file system l used to examine, locate, and manipulate files l composed of methods that can among other things l l obtain information about path through get. File. Name() l compare two paths using the equals() method instantiated by means of the get method of the Paths class l Example: Path p = Paths. get(“sample. txt”);
Files class l Files l provides support for common file and directory manipulations l exists(Path) l Verifies existence of a file or a directory is. Readable(Path), is. Writable(Path), is. Executable(Path) l Checks file accessibility is. Same. File(Path, Path) l Checks whether two paths locate the same file delete(Path) and delete. If. Exists(Path) l Delete files or directories copy(Path, Path)and move(Path, Path) § § § Copy and move files or directories
Files class (cont’d) l Files l allows for management of meta-data l size(Path) l size of specified file in bytes is. Directory(Path) l returns true if specified path is a directory get. Last. Modified. Time(Path) l File’s last modified time get. Owner(Path) l Owner of specified file read. Attributes(Path, Class) § § § Reads attributes of a file in one bulk operation
Basic File Attributes (Example) Path file =. . . ; Basic. File. Attributes attr = Files. read. Attributes(file, Basic. File. Attributes. class); System. out. println("creation. Time: " + attr. creation. Time()); System. out. println("last. Access. Time: " + attr. last. Access. Time()); System. out. println("last. Modified. Time: " + attr. last. Modified. Time()); System. out. println("is. Directory: " + attr. is. Directory()); System. out. println("size: " + attr. size());
Reading, writing and creating files using Files class l Buffered I/O methods for text files in Files class l new. Buffered. Reader(Path, Charset) § opens a file for reading returning a Buffered. Reader § Example: Charset charset = Charset. for. Name(“US-ASCII”); Buffered. Reader reader = Files. new. Buffered. Reader(file, charset); l new. Buffered. Writer(Path, Charset) § returns a Buffered. Writer § Example: Charset charset = Charset. for. Name(“US-ASCII”); Buffered. Writer writer = Files. new. Buffered. Writer(file, charset);
Reading, writing and creating files using Files class (cont’d) l Unbuffered I/O methods for text files in Files class l new. Input. Stream(Path, Open. Option. . . ) § returns an Input. Stream for reading bytes from file § Example: Input. Stream in = Files. new. Input. Stream(file); l new. Output. Stream(Path, Charset) § returns a Output. Stream § Example: Output. Stream out = Files. new. Output. Stream(file);