In I / O processing, the most common is the operation of files, and the classes related to files in Java.IO packages are: File, FileInputStream, FileoutputStream, RamdomAccessFile, and FileDescriptor; interfaces are: FileNameFilter.
File Description
The class file provides an attribute that describes a file object-independent manner. Below we introduce the various methods provided in the File.
◇ File or directory generation
Public file (String path); / * If Path is the actual path, the file object
/ * Represents a directory; if Path is the file name, the file object is represented by the file. * /
Public file (string path, string name); // path is the path name, name is the file name
Public file (file dir, string name); // DIR is the path name, name is the file name
◇ Documentation
String getName (); // Get the name of a file (excluding the path)
String getPath (); // Get the path name of a file
String getabsolutePath (); // Get absolute path names for a file
String getParent (); // Get the previous directory name of a file
String RenameTo (file newname); // rename the current file name as a given file
Complete route
◇ File attribute test
Boolean exists (); // Test if the file indicated by the File object exists
Boolean canwrite (); // Test if the current file can be written
Boolean canread (); // Test if the current file is readable
Boolean isfile (); // Test if the current file is a file (not a directory)
Boolean isdirectory (); // Test if the current file is a directory
◇ ordinary document information and tools
Long lastmodified (); // Get the time of the file last revision
Long length (); // Get the length of the file, in bytes
Boolean delete (); // Delete the current file
◇ directory operation
Boolean mkdir (); // Generate a path specified by the object according to the current object
String List (); // List the files in the current directory
[Example 4-3]
Import java.io. *; // Introduce all classes in java.io package
Public class filefiltertest {
Public static void main (string args []) {
File Dir = New file ("D: // EX"); // Represents a directory with the File object
Filter filter = new filter ("java"); // Generate a filter named Java
System.out.println ("List Java Files in Directory DIR);
String files [] = dir.list (file); // list the directory DIR, the file suffix name
All documents for Java
For (int i = 0; i File f = new file (dir, files [i]); / / for the file or directory of the directory DIR Create a File object IF (f.isfile ()) // If the object is the suffix of Java files, Print the file name System.out.println ("file" f); Else System.out.println ("Sub Directory" F); // If it is a directory Print the directory name } } } Class Filter Implements FileNameFiLTER { String extent; Filter (String Extent) { THIS.EXTENT = extent; } Public Boolean Accept (File Dir, String Name) { Return name.endswith ("." extent); // Return to the file's suffix } } Sequential processing of files Class FileInputStream and FileOutputStream are used to perform file I / O processing, and the methods they provide can open the files on the local host and perform read / write in order. For example, the following statement segment is the content in the file named text, and is displayed on the console until the file ends. FileInputstream Fis; Try { FIS = New FileInputStream ("Text"); System.out.print ("Content of Text IS:"); INT B; While ((b = fis.read ())! = - 1) // sequentially read the contents of the file text and assign Give the integer variable B until the end of the file. { System.out.print (CHAR) B); } } catch (filenotfoundexception e) { System.out.println (e); } catch (ioexception e) { System.out.println (e); }