对Java File类的操作-- delete

对Java File类的操作-- delete

@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
执行File.delete()时最好做一系列的验证。
import  java.io.File;
public   class  Delete {
    
public  Delete() {
        
//  TODO Auto-generated constructor stub
    }
    
/**
     * 
@param  args
     
*/
    
public   static   void  main(String[] args) {
        
if (args.length  !=   1 ){
            System.err.println(
" Usage:java Delete<file or directory> " );
            System.exit(
0 );
        }
        
        
try {
            delet1e(args[
0 ]);
        }
catch (IllegalArgumentException e){
            System.err.println(e.getMessage());
        }
    }
    
private   static   void  delet1e(String fileName) {
        File f 
=   new  File(fileName);
        
if ( ! f.exists())
            fail(
" Delete:no such file or firectory: "   +  fileName);
        
if ( ! f.canWrite())
            fail(
" Delete:write protected : "   +  fileName);
        
if (f.isDirectory()){
            String[] files 
=  f.list();
            
if (files.length  >   0 )
                fail(
" Delete:directory not empty: "   +  fileName);
        }
        
boolean  success  =  f.delete();
        
if ( ! success)
            fail(
" Delete: delete failed " );
    }
    
private   static   void  fail(String msg) throws  IllegalArgumentException {
        
//  TODO Auto-generated method stub
         throw   new  IllegalArgumentException(msg);
    }
}


Kyle Wang

你可能感兴趣的:(对Java File类的操作-- delete)