Delete a file or directory

Source: Internet
Author: User
1 package Io. fileop; 2 Import Java. io. *; 3/** 4 * delete a file or delete a directory 5 * @ author www.tfgzs.net 6 */7 public class Delete {8 9 public static void main (string [] ARGs) {10 string pathstring = "E:/360 downloads/123456.txt"; // file path 11 12 if (delete (pathstring) {13 system. out. println ("deleted successfully! "); 14} else {15 system. Out. println (" deletion failed! "); 16} 17} 18/** 19 * delete a file or delete a directory 20 * @ Param filename file path 21 * @ return success return true failure return false22 */23 public static Boolean Delete (string filename) {24 file = new file (filename); 25 if (! File. exists () {26 system. Err. println ("file already exists"); 27 return false; 28} 29 If (! File. canwrite () {30 system. err. println ("this file is protected"); 31 return false; 32} 33 If (file. isdirectory () {34 string [] files = file. list (); 35 if (files. length> 0) {36 system. err. println ("other files exist in this directory"); 37 return false; 38} 39} 40 return file. delete (); 41} 42}

 

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.