Java foundation classes of File IO class

Junior, currently poorly prepared, and re-learned! The following code, which has a detailed explanation of the meaning of each line of code represented by -

 

package IODemo;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class FileDemo {
    public static void main(String[] args) {

        // File表示一个文件或者目录
        //  "d:\\test\\test.txt" 或者 "d:/test/test.txt")

        File f1 = new File("d:\\test\\test.txt");
        if(!f1.exists()){  // determines whether the file exists, if it does not exist to create 
            the try { 
                f1.createNewFile (); // Create the file 
                System.out.println ( "file is successfully created!" ); 
            } The catch (IOException E) { 
                e.printStackTrace (); 
            } 
        } 
        // f1.isDirectory () returns true if the folder is not on the return false 
        System.out.println ( "f1 whether the file:" + f1.isFile ()); 

        file F2 = new new file ( "d: \\ \\ my the Test"); // this path in my folder into this folder have a file 
         boolean b = f2.delete (); // use the variable b to accept the results to see deleted 
        System.out. println ( "delete result is:" + b);   // returns false not delete this folder, you must want to delete this folder can delete the files inside the folder 

        String [] names = f2.list (); // list all the files in that directory names 
        System .out.println (Arrays.toString (names));   // print out the String array of things inside 

        file [] FS = f2.listFiles (); // list all files in the current directory, and the object to file returns 
        for (File F: 
                FS) {     // reinforcing loop that is for each of the set traversing a File Object File F 
            System.out.println ( "lenght =" + f.length ()); 
            System.out.println ( " = name "+ f.getName ()); 
            System.out.println ( " relative path = "+ f.getPath ()); 
            System.out.println ("Absolute path =" + f.getAbsolutePath ()); 
            System.out.println ( "whether hidden files =" + f.isHidden ()); 
            a Date DATE = new new a Date (f.lastModified ()); // Gets date last modified the file needs to be formatted with the date class 
            DateFormat DF = new new the SimpleDateFormat ( "HH: the MM: the SS");   // format 
            System.out.println ( "date:" + df.format (dATE)) ; 
        } 

        // folder operation 
        file F3 = new new : ( "Test \\ \\ Wang D" file ); 
        ; f3.mkdirs ()   // lower mkdirs () is to create a folder in the folder is not the case use and mkdir () does not only create a folder 
        System.out.println ( "Folder is created. "); 

        // rename and move folders
       //   f3.renameTo (new new File ( "d: \\ \\ Wang1 the Test"));
         // if placed elsewhere, then that is moving this folder 
        f3.renameTo ( new new file ( "D: \\ Wang" )); 


        
        // complementary printing filename filter f2 is 
        file F4 = new new file ( "D: \\ \\ My Test" ); 
        file [] files = f4.listFiles ( new new the FileFilter () { // new new filter out an interface 
            @Override
             public  Boolean Accept (file pathname) {
              return     pathname.getName () endsWith ( "txt.");.   // filter txt file ending 
             }
        });
        for (File f :
                files) {
            System.out.println(f.getName());
        }



    }
}

 

Guess you like

Origin www.cnblogs.com/lpss-75074038/p/11961152.html