Delete a Java class for a folder

Source: Internet
Author: User
Tags add arrays string tostring
/**
* @author www.nwu.edu.cn Minirice
* @author darkrice@gmail.com
* @version 1.2
*
*/

Package Cn.edu.nwu.io;

Import java.util.*;

public class File extends Java.io.File {


File (java.io.File file,string str) {
Super (FILE,STR);
}
File (String str1,string str2) {
Super (STR1,STR2);
}
File (String str) {
Super (STR);
}
File (Java.net.URI URI) {
Super (URI);
}
public static void Deletedirs (Java.io.File dir) throws exception{
M_root=dir;
m_dirs= new ArrayList ();
if (!m_root.isdirectory ()) {
throw new Exception ("Exception:\" "+m_root.tostring () +" \ is not a director);
}
else{
for (int i=0;i<m_dirs.size (); i++) {
System.out.println (((File) m_dirs.get (i)). ToString ());
}
Delete all Director
try{
M_dirs.add (M_root);
Mydelete ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
/**
*
* @param Dirpath String a director file path;
* @throws IOException If Dirpath is not a director file path
*/
public static void Deletedirs (String dirpath) throws exception{
M_root=new Java.io.File (Dirpath);
Deletedirs (M_root);
}

public static void Deletesubdirs (String dirpath) throws exception{
M_root=new Java.io.File (Dirpath);
Deletesubdirs (M_root);
}


public static void Deletesubdirs (Java.io.File dir) throws exception{
M_root=dir;
m_dirs= new ArrayList ();
Deletedirs (M_root);
if (!m_root.isdirectory ()) {
throw new Exception ("Exception:\" "+m_root.tostring () +" \ is not a director);
}

else{
for (int i=0;i<m_dirs.size (); i++) {
System.out.println (((File) m_dirs.get (i)). ToString ());
}
try{
Mydelete ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
/*
* Visit all a director and save them in a list
*/
private static void Visitall (Java.io.File temproot) {
M_dirs.add (Temproot);
System.out.println ("Beginning of Function:" +m_dirs.size ());

Java.io.file[] Dirs=temproot.listfiles ();
if (dirs!=null) {

List dirslist=arrays.aslist (dirs);
while (Dirslist!=null) {
if (dirslist==null) {
try{
Temproot.delete ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
else{

M_dirs.addall (dirslist);
System.out.println (M_dirs.size ());
/*
for (int i=0;i<dirs.length;i++) {//print dirs
System.out.println ("NO.") + ++counter+ ":" +dirs[i].tostring ());
}
*/
System.out.println (M_dirs.size ());

for (int i=0;i<dirslist.size (); i++) {
temproot= (java.io.File) dirslist.get (i);
Visitall (Temproot);
/*
Dirs=temproot.listfiles ();
List dirslist= arrays.aslist (dirs);
*/
System.out.println (Temproot.tostring ());
}
}
return true;
//}
}
}
/*
* Do Delete
*/
private static void Mydelete () throws exception{
M_dirs.add (M_root);
Visitall (M_root);

if (m_dirs!=null) {

System.out.println ("Before deleting:" +m_dirs.size ());
System.out.println (M_dirs.size ());
for (int i=m_dirs.size () -1;i>=0;i--) {
Java.io.File f= (java.io.File) m_dirs.remove (i);
File f= (file) m_dirs.get (i);
System.out.println (F.tostring ());
String filename=f.tostring ();

if (!f.delete ()) {
throw new Exception ("Exception:delete file" + filename+ "false!");
System.out.println ("Delete file/folder" +filename+ "Failed!");
}
}
}
else{
throw new Exception ("Exception:read file List of" +m_root.tostring () + "false!");
System.out.println ("Arraylist==null");
}
}
/*
* Root Director
*/
private static Java.io.File M_root;

private static ArrayList M_dirs;
private static int counter=0;

public static void Main (string[] args) {//test class

File file =new file ("j:/Graduation design/test class/test file");
try{
File.deletedirs ("j:/Graduation design/test class/Test document");
File.deletedirs (file);
File.deletesubdirs ("j:/Graduation design/test class/Test document");
File.deletesubdirs (file);
File F=new file ("F:/test.txt");
F.delete ();
}
catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Delete success ~");
}
}//:~

Finished, the problem is not difficult, is the first attempt to use inheritance and exceptions.


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.