编程知识 cdmana.com

Java file operation object IO stream

//File object   C:/Users/28528/Desktop/java File to practice 
        // Construct a FIle object , Incoming file path ( Absolute path , Relative paths )
        // The absolute path is the full path starting with the root directory   Such as  C:/Users/java File to practice 
        // Relative paths   Such as  /usr/bin/javac
        // Be careful Windows Platform use \ As path separator ,
        // stay Java You need to use... In the string \\ It means a \.Linux Platform use / As path separator 
        // It can be used . Represents the current directory ,.. Indicates the parent directory .
        File f1 = new File("sub\\javac"); //  The absolute path is C:\Docs\sub\javac
        File f2 = new File(".\\sub\\javac"); //  The absolute path is C:\Docs\sub\javac
        File f3 = new File("..\\sub\\javac"); //  The absolute path is C:\sub\javac
        File f=new File("C:/Users/28528/Desktop/java File to practice ");
        File f4=new File("..");
        System.out.println(f1+"\n"+f2+"\n"+f3+"\n"+f);
        System.out.println(f1.getCanonicalPath());
        String file=File.separator;
        //File.separator  Print according to the current platform  "\"  or  "/"  Static variables separator System separator 
        System.out.println(file);
        //File There are three forms of objects     1.getPath 2.getAbsolutePath 3.getCanonicalPath
        //1  Returns the path passed in by the constructor 
        //2  Return to absolute path 
        //3  Return specification path   Similar to the absolute path 
//File  Calling method 
        //1  Judge File Whether the path in the object exists 
        File f1=new File("C:/Users/28528/Desktop/java File to practice ");// Catalog 
        File f2=new File("C:\\Users\\28528\\Desktop\\demo1.txt");// file 
        System.out.println(f2.isFile());// Determine whether a file exists  true false
        System.out.println(f1.isDirectory());// Determine whether a directory exists  true false
        //2  obtain File Object's file permissions and size 
//         use File Object to get a file , You can also further determine the permissions and size of the file :
//        boolean canRead(): Is it readable? ;
//        boolean canWrite(): Is it possible to write ;
//        boolean canExecute(): Is it executable ;
//        long length(): File byte size .
        System.out.println(f2.length());
//File Create and delete files 
        String name1=" Default folder 1",name2="txt";// The default file name is followed by the suffix 
        System.out.println(" Whether to customize the file name (y):");
        char shu=in.next().charAt(0);
        if(shu=='y'){
            System.out.println(" Please enter filename :");
            name1=in.next();// Enter the name of the created file 
            System.out.println(" Please enter the suffix :");
            name2=in.next();// Enter the suffix of the created file 
        }
        File file=new File("C:/Users/28528/Desktop/"+name1+"."+name2);
        System.out.println(file);// Output file name 
        if(file.isFile())// Determine if there is a file 
        {
            if(file.delete()){// There are files to delete 
                System.out.println(" The file has been deleted ");
            }
        }else{// Create a file if it doesn't exist    The created file name needs to be suffixed by itself 
            file.createNewFile();// Create a file 
            System.out.println(" File created successfully ");
            System.out.println(" Delete created file (y):");
            shu=in.next().charAt(0);
            if(shu=='y'){
                if(file.delete()){
                    System.out.println(" File deleted ");
                }
            }
        }

Automatically delete after running

{tabs-pane label=" Code "}

//File The temporary file 
        // Prefix and suffix of temporary files 
        File file=File.createTempFile(" This is a temporary file ",".txt");
        //JVM Automatically delete on exit 
        file.deleteOnExit();
        System.out.println(file.isFile());// Judge whether the file exists 
        System.out.println(file.getAbsolutePath());// Return the absolute path of the file 

{/tabs-pane} {tabs-pane label=" picture "}

{/tabs-pane}

{tabs-pane label=" Code "}

//File  Get the file and subdirectory names under the directory 
        File file=new File("C:/Users/28528/Desktop/");
        File[] filelist=file.listFiles();// Get all files and subdirectories 
        for(File f:filelist){// utilize foreach Loop print all files and subdirectories 
            System.out.println(f);
        }
        //FilenameFilter  The live directory of files that meet the file requirements returns 
        File[] filelist2=file.listFiles(new FilenameFilter(){
            @Override
            public boolean accept(File dir,String name){
                return name.endsWith(".docx");// return true Indicates that the file was received 
                //endsWith   Filter out the specified suffix 
                //startsWith   Filter out the specified prefix 
            }
        });
        printlnfiles(filelist2);//foreach Method of circular printing 

{/tabs-pane} {tabs-pane label="printlnfiles Method "}

public static void printlnfiles(File[] f){
        System.out.println("````````````````````");
        if(f!=null){
            for(File file:f){
                System.out.println(file);
            }
        }
        System.out.println("````````````````````");
    }

Similar to file manipulation ,File Object represents a directory , You can create and delete directories in the following ways :

boolean mkdir(): Create current File Object represents the directory ; boolean mkdirs(): Create current File Object represents the directory , And create a parent directory that doesn't exist when necessary ; boolean delete(): Delete the current File Object represents the directory , The current directory must be empty to delete successfully . {/tabs-pane} {tabs-pane label=" picture "}

{/tabs-pane}

This article altogether 700 Number of words , Average reading time ≈ 2 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824215w.html

Scroll to Top