Java File class

Source: Internet
Author: User

 PackageIodemo;ImportJava.io.File;ImportJava.io.FileFilter;Importjava.io.IOException;Importjava.util.Arrays;/*** @Title: Iodemo * @Description: Use of the file class *@authorCRAZY-ZJ * @data September 21, 2017 pm 2:31:10 * @book Crazy Java Handout (third edition):*/ Public classIodemo { Public Static voidMain (string[] args) {//The file class represents a document or directory        /*** Three kinds of separators: * 1. " F:/test/test.txt "\ itself means escape so you cannot use \ only/* 2."         F: "+file.separator+" Test "+file.separator+" test.txt "system-related default name-delimiter character, expressed in a convenient way as a string. */File F1=NewFile ("F:" +file.separator+ "Test" +file.separator+ "Test.txt"); if(!f1.exists ()) {//determine if the file exists, F1 this object has been created to exist            Try{f1.createnewfile ();//CreateNewFile () Create a new fileSystem.out.println ("File creation succeeded! "); } Catch(IOException e) {//Basically, all file stream operations will report IOException exceptions.E.printstacktrace (); }        }        //Isfile () determines whether the file Isdirectory () determines whether it is a folderSystem.out.println ("is folder:" +f1.isdirectory ()); File F2=NewFile ("F:\\test\\test");//to delete this folder, you must first delete the files in the folder, no delete failed        Booleanb =F2.delete ();                System.out.println (b); String[] Names= F2.list ();//List all filenames under the current directorySystem.out.println (arrays.tostring (names)); File[] FS=f2.listfiles ();//lists all files in the current directory and returns them as file Objects         for(File f:fs) {System.out.println ("File name:" +f.getname ()); System.out.println ("File Length:" +f.length ()); System.out.println ("Relative path:" +F.getpath ()); System.out.println ("Absolute path:" +F.getabsolutepath ()); }//System.out.println ("--------------"); //find a specific fileFile f5 =NewFile ("F:\\test\\test"); /*file[] files = f5.listfiles (new FileFilter () {@Override public boolean accept (File p                Athname) {return pathname.getname (). EndsWith (". txt"); }            });*/file[] Files= F5.listfiles ((pathname), Pathname.getname (). EndsWith (". txt"));//lambda expression notationSystem.out.println ("-------------");  for(File f:files) {System.out.println (F.getname ()); }    }}
    1. File class: An abstract representation of files and directory names
    2. The file class can be used to create, delete, rename, get paths, create time, and so on, and is the only action class related to the file. See the Java API
    3. How to manipulate the file class:

1.public static Final String separator represents the path delimiter "\"

2.public file (String pathname) constructs an instance of the file class to pass in the path

3.public Boolean createnewfile () to create a new file

4.public Boolean Delete () Delete file

5.public Boolean isdirectory () determines whether the given path is a folder

6.public boolean isfile () determines whether the given path is a file

7.public String list () lists the files in the folder

8.public file[] Listfiles () lists all files in the folder

9.public boolean mkdir () Create a new folder

10.public boolean renameto (file Test) to rename files

11.public Ling Length () returns the file size

12public GetPath () path name string

Java File class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.