重命名的方法:

File booksIndexDirBak = new File(BOOKS_INDEX_PATH);
File dest = new File(BOOKS_INDEX_PATH + "_bak");
if (!booksIndexDirBak.renameTo(dest)) {
   System.out.println("error.");
}
else {
   System.out.println("OK.");
}

删除文件的方法:
file.delete();


删除文件夹的话,前提是这个文件夹是空的,如果里面还有文件是删除不掉的.需要通过遍历,先将里面的文件删除掉,然后再删除掉这个文件夹.
方法:

public   static   boolean   deletefile(String   delpath)   throws   FileNotFoundException, IOException   { 
        try   { 
            File   file   =   new   File(delpath); 
            if   (!file.isDirectory())   { 
                file.delete(); 
            } 
            else   if   (file.isDirectory())   { 
                String[]   filelist   =   file.list(); 
                for   (int   i   =   0;   i   <   filelist.length;   i++)   { 
                    File   delfile   =   new   File(delpath   +   "\\ "   +   filelist[i]); 
                    if   (!delfile.isDirectory()) 
                        delfile.delete(); 
                    else   if   (delfile.isDirectory()) 
                        deletefile(delpath   +   "\\ "   +   filelist[i]); 
                } 
                file.delete(); 
            } 
        } 
        catch   (FileNotFoundException   e)   { 
            Log.debug( "deletefile()   Exception: "   +   e.getMessage()); 
        } 
        return   true; 
    } 

相关文章