Java deletes an object

Source: Internet
Author: User
// Delete the files in the buffer folder
String S = "D: // TXT // inbuffer //" + fileinfo [0] + "//" + fileid; // absolute path of the file
File file = new file (s );
If (file. exists ()){
Boolean d = file. Delete ();
If (d ){
System. Out. Print ("deleted successfully! ");
} Else {
System. Out. Print ("deletion failed! ");
}
}

Notes for deleting files in Java: 1. the paths cannot contain characters that are considered invalid by Java, such;

2. Ensure that the file is no longer used before the delete operation, that is, the file resource is released!

Java deletes files and folders:

1. Files in the folder;

2. subfolders (with files) in the folder );

3. subfolders in the folder (empty folder );

Routine:

Package test;

Import java. Io. file;

Public class deletefile {
// Delete a folder
// Complete absolute path of the param folderpath folder

Public static void delfolder (string folderpath ){
Try {
Delallfile (folderpath); // delete all contents
String filepath = folderpath;
Filepath = filepath. tostring ();
File myfilepath = new file (filepath );
Myfilepath. Delete (); // delete an empty folder
} Catch (exception e ){
E. printstacktrace ();
}
}

// Delete all objects in the specified folder
// Complete absolute path of the param path folder
Public static Boolean delallfile (string path ){
Boolean flag = false;
File file = new file (PATH );
If (! File. exists ()){
Return flag;
}
If (! File. isdirectory ()){
Return flag;
}
String [] templist = file. List ();
File temp = NULL;
For (INT I = 0; I <templist. length; I ++ ){
If (path. endswith (file. separator )){
Temp = new file (path + templist);
} Else {
Temp = new file (path + file. Separator + templist);
}
If (temp. isfile ()){
Temp. Delete ();
}
If (temp. isdirectory ()){
Delallfile (path + "/" + templist); // Delete the files in the folder first
Delfolder (path + "/" + templist); // Delete the empty folder.
Flag = true;
}
}
Return flag;
}
Public static void main (string [] ARGs ){
Deletefile T = new deletefile ();
Delfolder ("D:/1 ");
System. Out. println ("done ");
}
}










Related Article

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.