To sum up the knowledge and concepts related to the basic classes for reading and writing Java I/O files, it is a difficult task for programmers to create a good input/output system. The challenge comes from all possibilities, there are not only various source and acceptor files, consoles, network connections, etc.), but also different ways to communicate with them in order, random access, buffering, binary, by character, by line, by word, etc ).
Java I/O mainly includes the following layers:
1. File
It is independent from the system platform and uses its constructor to create the corresponding File object. Then, it calls the method to perform operations on all attributes of the File.
Constructor:
- File (String path)
- File (String path, String FileName)
- File (File dir, String name)
Purpose: The File class provides a machine-independent method to describe the attributes of a File object. The File or directory description can be obtained through the method provided by the File class, this mainly includes the name, path, readability, writability, and file length. It can also generate new directories, change file names, delete files, and list all files in a directory.
- Public static void main (String [] args) throws IOException {
- File f = new File ("dir ");
- F. createNewFile (); // create a. txt file.
- F. mkdir (); // create a directory named .txt
- /*
- * Use absolute path
- *
- * File f = new File ("D: \ dir \ src \ A. java ");
- *
- * F. createNewFile ();
- */
- /*
- * Cross-platform use
- *
- * Obtain the corresponding delimiter File fDir = new File (File. separator) based on different operating systems );
- *
- * String strFile = "dir" + File. separator + "src" + File. separator + "A. java ";
- *
- * File f = new File (fDir, strFile );
- *
- * F. createNewFile ();
- *
- * F. delete (); // delete a file or directory
- *
- * // F. deleteOnExit ();
- */
- /*
- * Create a temporary file in the default temporary file directory
- *
- * For (int I = 0; I <5; I ++)
- *
- *{
- *
- * File f = File. createTempFile ("winTemp", ". tmp ");
- *
- * F. deleteOnExit (); // Delete when exiting
- *
- *
- *
- *}
- */
- /*
- * List all subdirectories and file names in a specified directory
- */
- File fDir = new File (File. separator );
- String strFile = "dir" + File. separator + "src ";
- File f = new File (fDir, strFile );
- String [] names = f. list ();
- For (int I = 0; I <names. length; I ++ ){
- System. out. println (names [I]);
- }
- // If a filter exists, FilenameFilter is an interface.
- File dir = new File (File. separator );
- String filepath = "dir" + File. separator + "src ";
- /**
- * Dir
- * The upper-level abstract path. If dir is null, the program automatically calls the File constructor of a single parameter and applies the filepath path to the File but the constructor.
- * If dir is //, the path is the root directory of the disk where the file is located.
- */
- File f = new File (dir, filepath );
- If (f. exists ()){
- } Else {
- F. mkdirs ();
- }
- String [] names = f. list (new FilenameFilter () {// implements the FilenameFilter interface's Anonymous class, and implements the accept method to filter files
- @ Override
- Public boolean accept (File dir, String name ){
- System. out. println (name. indexOf (". java "));
- Return name. indexOf (". java ")! =-1;
- }
- });
- For (int I = 0; I <names. length; I ++ ){
- System. out. println (names [I]);
- }
- }