JAVA file Class (Traversal of files, creation, deletion)

Source: Internet
Author: User

File class constructor

File F1=new ("h://asc//");//The path of the incoming files/directories is "f2=new" file (F1, "test.txt");//The first parameter is a directory file, the second parameter is the file to be created in the current F1 directory

File.list (); Get a string array of all files/directories under the file folder

String []listrings=f1.list (); for (int i = 0; i < listrings.length; i++) {System.out.println (listrings[i]);}

Use the list () function to traverse the folder and recursively display the files/folders under the folder

public static void Dfs (string filename) {//Java case-sensitive string class first uppercase file F = new File (filename); String namelist[] = F.list (); for (int i = 0; i < namelist.length; i++) {File Fchild = new file (filename + "/" + Namelis T[i]); for (int j = 0, J < Deep; J + +) System.out.print ("   "); if (!fchild.isdirectory ()) System.out.println (namelist[ I]);//File Print else {//directory System.out.println (Namelist[i] + "/");//first print and then recursively call itself DFS (filename + "/" + Namelist[i]);}}

  

File.isfile () Determines whether the current file file File.isdirectory () is a directory file.exist (); Whether this file or directory exists

File F1=new file ("h://asc//"),//file f2=new file (F1, "test.txt"),//file f3=new file ("H://notexistdir");// System.out.println (F1.isfile ()); System.out.println (F2.isfile ()); System.out.println (F3.isfile ()); System.out.println (F1.isdirectory ()); System.out.println (F2.isdirectory ()); System.out.println (F3.exists ());
Operation Result:
Falsetruefalsetruefalsefalse

  

  

File/directory creation, deletion

The function file.createnewfile () creates this file object files if there is already a file in between, CreateNewFile returns Fasle creating the files failed

function File.mkdir () Creates a file directory, one drawback of this function is that you can only create folders under existing paths if your file object path does not exist on your computer, you will create a failure. Using File.mkdirs () even if the file path does not exist, the corresponding folder directory is automatically created to guarantee the creation of the Document object.

We recommend that you use Mkdirs () to create a folder

public void Creatnewfile (String path) {//Create new file \ Directory file = new (path), if (File.exists ()) {//To determine if the file under this path exists no System.ou T.println ("This file already exists! "); return;} System.out.println ("Create files/directories under this path?") "); SYSTEM.OUT.PRINTLN ("Input 1 Create file directory                 input 2 create file"); Scanner Scanner = new Scanner (system.in); int j = Scanner.nextint (); if (j = = 2) {try {file.createnewfile ();//Create File} catch (I Oexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else {file.mkdirs ();//Create Directory}}

File.delete () can implement the deletion of files/folders, delete () function Although can delete files, but in the deletion of folders, can only implement the deletion of the empty directory, if you want to delete the folder below the file/folder will be deleted failure

If you want to implement the removal of such a folder you can use the DFS traversal file directory +delete () implementation

     Public BooleanDeleteFile (String path) {//Delete File | directory (linked delete)File File =NewFile (path); if(!file.exists ()) {System.out.println ("File does not exist!" "); return false;        } dfsdelete (path); return true; }     Public voiddfsdelete (String path) {File file=NewFile (path); if(File.isfile ()) {//If this file object is a document, delete it directly .File.delete (); return; }        //when file is a folder, you get a string array of corresponding files in the folder, recursively call itself, implement depth first deleteString [] list=file.list ();  for(inti = 0; i < list.length; i++) {dfsdelete (path+file.separator+List[i]); }//when all the files in the folder are deleted, this folder is already an empty folder and can be deleted directly using delete () .File.delete (); return; }    

JAVA file Class (Traversal of files, creation, deletion)

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.