【】 Java IO (1) - 类 File

【】 Java IO (1) - 类 File

 


 

In Java, File class java.io package is subject only mapping a disk file itself . File class can obtain information about the file (see the file name, path, access, modification date, etc.), but the File class can not read information from and write information to a file in a file .

1. Three File class constructor

1  // File (File parent, String child): Creates a new File instance from a parent abstract pathname and a child pathname string. 
2 File parent = new new File ( "E: \\ Test" );
 . 3 File Child = new new File (parent, "demo.txt" );
 . 4  
. 5  // File (String pathname): given by the path name string converted into an abstract pathname to create a new File instance. If the given string is a null string, the result is empty abstract pathname. 
6 File File = new new File ( "E: \\ \\ demo.txt the Test" );
 7  
8  // File (String parent, String child): Creates a new File from a parent pathname string and a child pathname string instance. 
. 9 String parent = "E: \\ \\ Test" ;
 10 String = Child "demo.txt"11 File file = new File(parent,child);

Note: Windows operating system path, such as E: \ demo \ hello.java, in a Java program, its path is written as E: /demo/hello.java or E: \\ demo \\ hello.java

 

The main function 2. File class

(1) get the file attributes

. 1  Import java.io.File;
 2  Import java.util.Date;
 . 3  
. 4  public  class File_test {
 . 5      public  static  void main (String [] args) {
 . 6          File F = new new File ( "E: \\ \\ Hello Test .doc ");     // abstract path name" E: \\ test \\ hello.doc " 
7          System.out.println (" get file E: \\ test \\ hello.doc of information: " );
 8          System.out.println ( "-------------------------------" );
 9          System.out.println ( "file length: "+ f.length () +" bytes " );
 10         System.out.println ( "judgment of the File object is not a file:" + (f.isFile () "is a file":? "Is not a file" ));
 11          System.out.println ( "File object is not the judge directory: "+ (? f.isDirectory () " directory ":" not a directory " ));
 12          System.out.println (" is readable: "? + (f.canRead () " can be read ": "unreadable" ));
 13          System.out.println ( "can write:" + (f.canWrite () "can be written":? "writing" ));
 14          System.out.println ( "whether to hide:" + (? f.isHidden () " is a hidden file": "No hidden file" ));
 15          System.out.println ( "last modified:" + new new a date (f.lastModified ()) );
 16          System.out.println ( "File name: "+ f.getName ());
 . 17          System.out.println (" Parent File Path: "+ f.getParent ());
18 is          System.out.println ( "abstract pathname into the path name string:" + f.getPath ());     // equivalent f.toString ()
 . 19          System.out.println ( "determines whether the abstract pathname absolute path: "+ (f.isAbsolute ()? " is the absolute path ":" is not an absolute path " ));
 20          System.out.println (" absolute file path: "+ f.getAbsolutePath ());
 21          System .out.println ( "-------------------------------" );
 22 is      }
 23 is }

operation result

Get File E: \ test \ hello.doc of information:
 ------------------------------- 
file length: 9216 bytes 
determine whether the file object is not a file: the file 
to determine the file object is not a directory: not a directory 
is readable: to read 
is writable: writable 
is hidden: hidden files are not 
the last modified date: Mon Jul 01 21:20 : 47 CST 2019 
file name: hello.doc 
file parent path: E: \ test 
abstract pathname into a pathname string: E: \ test \ hello.doc 
determine whether abstract pathname is absolute path: absolute path 
absolute file path: E: \ the Test \ hello.doc
 -------------------------------

boolean setReadOnly public () : Set the file can only be read, not write.

. 1  public  static  void main (String [] args) {
 2          File F = new new File ( "E: \\ test \\ hello.doc"); // abstract path name "E: \\ test \\ hello.doc " 
. 3          f.setReadOnly ();
 . 4          System.out.println (" is readable: "+ (f.canRead ()? " readable ":" unreadable " ));
 . 5          System.out.println ( "can write:" + (f.canWrite () "can be written":? "writing" ));
 6      }

operation result

It is readable: readable 
is writable: unwritable

 

(2) create and delete files, create and delete directories, directory traversal list () and list ()

String [] List () : This method returns an array of strings represents all files in a directory and subdirectory names consisting of the File object, if the call is not a directory File object, null is returned.

Tip: List () method returns an array contains only the file name, and does not include a path. But it does not guarantee the same strings in the resulting array will appear in a specific order, in particular, that they appear in alphabetical order.

. 1  Package test0607;
 2  
. 3  Import java.io.File;
 . 4  Import java.io.IOException;
 . 5  Import java.util.Date;
 . 6  
. 7  public  class File_test {
 . 8      public  static  void main (String [] args) throws IOException {
 . 9          of NF = file new new file ( "E: /test/new_file.doc"); // create a link to the file E: /test/new_file.doc file object 
10          iF (nf.exists ()) // determine whether the file exists 
11          {
 12             nf.delete (); // exist delete 
13          }
 14          nf.createNewFile (); // re-create the file 
15  
16          File dir = new new File ( "E: / test01"); // create a link to the directory E: / test01 File object 
. 17          iF (dir.exists ()) // determines whether there is the directory 
18 is          {
 . 19              dir.delete (); // exist delete 
20 is          }
 21 is          dir.mkdir (); // then create the directory 
22 is  
23 is          File LF = new new File ( "E: / Test /"); //The need to create a File object to traverse the 
24-          System.out.println ( "file type \ t File size \ t file name" );
 25          System.out.println ( "--------------- --------------- " );
 26 is          String the fileList [] = lf.list (); // call list with no parameters () method 
27          for ( int I = 0; I <fileList.length; i ++) { // character through the returned array 
28              System.out.print (( new new file ( "E: / the Test /", fileList [i])) isFile () "file" +. "? \ t ":" folder "+" \ T " );
 29              of System.out.print (( new new file (" E: / Test / ", the fileList [I])). length () +" bytes "+ "\ t");
30             System.out.println (the fileList [I]);            
 31 is              // Since the character array list () method returns the name of the file includes only, so in order to get the file type and size, must be converted to a File object then call its methods. 
32          }
 33 is          System.out.println ( "------------------------------" );
 34 is      }
 35 }

operation result

File Name File Type Size
 ---------------------------------- 
folder dir01 0 byte 
folder word 0 section dir02 
file good.bmp 0 byte 
file hello.doc 9216 byte 
file hi.txt 0 byte 
file byte 0 new_file.doc
 -------------------- --------------

 

String [] list (the FilenameFilter filter) : the same effect as the method list () method, except that the array returned contains only the files and directories of the filter in line with filter, if the filter is null, then accept all names.

First create a file filter , the filter must implement  java.io.FilenameFilter an interface, and allows the specified file type accept () method.

. 1  Import java.io.File;
 2  Import java.io.FilenameFilter;
 . 3  Import java.io.IOException;
 . 4  Import java.util.Date;
 . 5  
. 6  class DocFilter the implements FilenameFilter
 . 7  {
 . 8      // implement FilenameFilter interfaces 
. 9      @Override
 10      public  Boolean Accept (file the dir, String name)
 . 11      {
 12 is          // specified allowed document type 
13 is          return name.endsWith ( "TXT.") || name.endsWith ( "BMP." );
 14      }
15  }
 16  
. 17  public  class File_test03 {
 18 is      public  static  void main (String [] args) throws IOException {
 . 19          File LF = new new File ( "E: / Test /"); // Create a File object needs to traverse 
20 is          the System.out .println ( "file type \ t file size \ t file name" );
 21          System.out.println ( "------------------------- ----- " );
 22 is          String the fileList [] = lf.list ( new new DocFilter ()); // call list with no parameters () method 
23 is          for ( int0 = I; I <fileList.length; I ++) { // iterate return character array 
24              of System.out.print (( new new File ( "E: / Test /", the fileList [I])) isFile ().? "file" + "\ t": "folder" + "\ T" );
 25              of System.out.print (( new new file ( "E: / Test /", the fileList [I])). length () + "byte" + "\ T" );
 26 is              System.out.println (the fileList [I]);            
 27              // Since the list () method returns a character array containing only a file name, so in order to get the file type and size, File must be converted to an object and then call its methods. 
28          }
 29          System.out.println ( "------------------------------" );
 

operation result

File Name File Type Size
 ------------------------------ 
file good.bmp 0 byte 
file byte 0 hi.txt
 ------------------------------

 

Guess you like

Origin www.cnblogs.com/yadiel-cc/p/11117276.html