package com.jkk.io;
import java.io.*;
/**
*这个类是一个使用静态方法delete()和一个删除指定文件或目录的独立程序
*
**/
public class Delete{
/**
*独立程序的main()方法,检测其参数后
*调用 Delete.delete() 进行删除操作
*
**/
/**
*用于删除文件或目录的静态方法,由main()调用
*也可有其它程序调用
*删除前首先确定文件或,目录是否可删除
*如有问题,在于抛出异常IllegalArgumentException
**/
public static void delete(String filename){
File f =new File(filename);
//确定文件或目录是否存在,是否写保护
if (!f.exists())fail( "Delete no such file or directory: "+filename);
if (!f.canWrite())fail( "Delete:write protected: "+filename);
if (f.isDirectory()){
String[] files= f.list();
if (files.length> 0)
fail( "Delete :directory no empty: "+filename);
}
//如果通过了所有测试,删除
boolean success=f.delete();
if (!success)fail( "Delete:deletion failed ");
}
//抛出异常的简便方法
protected static void fail(String msg)throws IllegalArgumentException {
throw new IllegalArgumentException(msg);
}
public static void main(String[] args){
if (args.length!=1){
System.err.println( "Usage:java Delection <file or directory> ");
System.exit(0);
try{
delete(args[0]);
}
//调用delete()显示抛出错误
catch (IllegalArgumentException e){
System.err.println(e.getMessage());
}
}
}
}
/**
* 这个程序在我编译后运行执行时会出现这样的问题
*Exception in thread "main " java.lang.NoClassDeFoundError:Delete(wrong name:com/jkk/io/Delete)为什么?
*/
------解决方案--------------------
包的问题
运行的时候把
------解决方案--------------------
javac -d . Delete.java 编译
java com.jkk.io.Delete 运行