Java learning - file IO operations

1. Traverse Folder

Traversing the C: \ WINDOWS directory of all files (not including folders), file and print out their minimum and maximum size (bytes)

Knowledge Point: listFiles (), length (), isFile ()

. 1  Package IOTest;
 2  Import java.io.File;
 . 3  
. 4  public  class Test {
 . 5      public  static  void main (String [] args) {
 . 6  
. 7          File F = new new File ( "C: / the WINDOWS" );
 . 8          File FS [ ] = f.listFiles ();
 . 9          // statistical minimum and maximum values to use temporary variables 
10          int max = 0 ;
 . 11          int min = ( int ) (Math.pow (2, 31 is) -. 1 );
 12 is          int MAX_INDEX = 0 ;
 13         int min_index = 0;
14         
15         for (int i = 0; i < fs.length; i++) {
16             if (fs[i].isFile()) {
17                 if (fs[i].length() > max) {
18                     max = (int) fs[i].length();
19                     max_index = i;
20                 }
21                 if (fs[i].length() < min && fs[i].length() != 0) {// 最小文件不能为0字节
22                     min = (int) fs[i].length();
23                     min_index =I;
 24                  }
 25              }
 26 is          }
 27          System.out.printf ( "maximum file was% s, size% d bytes \ n-" , FS [MAX_INDEX], max);
 28          System.out.printf ( "Minimum File was% s,% d bytes size " , FS [min_index], min);
 29      }
 30 }

Renderings:

2. Traverse Folder (Advanced)

All files in the specified folder (including all sub-folder files under)

Knowledge Point: recursion, global variables, the first question of the code can be modified

. 1  Package IOTest;
 2  
. 3  Import java.io.File;
 . 4  
. 5  public  class Test {
 . 6      // forced by the following global variables 
. 7      static  int max = 0 ;
 . 8      static  int min = ( int ) (Math.pow (2, 31 is) -. 1 );
 . 9      static  int MAX_INDEX = 0 ;
 10      static  int min_index = 0 ;
 . 11      static String maxFile = null ; // name of the largest file 
12 is      static String minFile=null;
13     
14     public static void func(File f) {
15         if (f.exists()) {
16             File fs[] = f.listFiles();        
17             for (int i = 0; i < fs.length; i++) {
18                 if (fs[i].isFile()) {//若是文件
19                     if (fs[i].length() > max) {
20                         max = (int) fs[i].length();
21                         max_index = i;
22                         = maxFile String.valueOf (FS [MAX_INDEX]);
 23 is                      }
 24                      IF (! FS [I] .length () <min && FS [I] .length () = 0) { // minimum file can not be 0 bytes 
25                          min = ( int ) FS [I] .length ();
 26 is                          min_index = I;
 27                          minFile = String.valueOf (FS [min_index]);
 28                      }
 29                  } the else { // if the folder, recursive calls continue searching func 
30                      FUNC (FS [I]);
 31 is                  }
 32             }
 33 is          }
 34 is      }
 35      
36      public  static  void main (String [] args) {
 37 [  
38 is          File F = new new File ( "D: / Program Files (the x86)"); // if C disc pointer exception may occur, is estimated file access issues 
39          FUNC (F);
 40          System.out.printf ( "maximum file was% s, size% d bytes \ n-" , maxFile, max);
 41 is          System.out.printf ( "minimum file was% s, size% d bytes \ n-" , minFile, min);
 42 is      }
 43 is }

Renderings:

Guess you like

Origin www.cnblogs.com/gilgamesh-hjb/p/12180165.html