public class file {
public static void main(String[] args){
if (args.length !=1){
fail("err!!");
System.exit(0);}
try {delete(args[0]);}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
public static void delete(String filename){
File f=new File(filename);
if (!f.exists()) fail("文件不存在!");
if(!f.canWrite()) fail("文件已经写保护!");
if (f.isDirectory()) {
String[] dir=f.list();
if (dir.length>0){
fail("该目录非空!");
}
boolean suc =f.delete();
if (!suc) fail("文件删除失败");
}
} protected static void fail(String msg) throws IllegalArgumentException{
throw new IllegalArgumentException(msg);
}
}
运行:javac file.java通过
问题1: java file a.txt//删除当前目录下的a.txt文件,这样做对吗?
结果: Exception in thread "main" java.lang.NoClassDefFoundError: file ///为何???
public static void main(String[] args){
if (args.length !=1){
fail("err!!");
System.exit(0);}
try {delete(args[0]);}
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
public static void delete(String filename){
File f=new File(filename);
if (!f.exists()) fail("文件不存在!");
if(!f.canWrite()) fail("文件已经写保护!");
if (f.isDirectory()) {
String[] dir=f.list();
if (dir.length>0){
fail("该目录非空!");
}
boolean suc =f.delete();
if (!suc) fail("文件删除失败");
}
} protected static void fail(String msg) throws IllegalArgumentException{
throw new IllegalArgumentException(msg);
}
}
运行:javac file.java通过
问题1: java file a.txt//删除当前目录下的a.txt文件,这样做对吗?
结果: Exception in thread "main" java.lang.NoClassDefFoundError: file ///为何???
解决方案 »
- 为什么会报出NullPointerException
- 如何实现有三种状态的JCheckBox?
- 急,请问如何解决:Exception in thread "main"java.lang.NoClassDefFoundError: org/eclipse/jface/viewers/ISelectionChangedListener
- 知其然,还要直其所以然--ajax和struts结合的问题
- 又是该死不懂的基础!
- BES VisiBroker Edition 6.5 中如何配置GateKeeper ?[解决问题100分]
- 如何取出String中的字符并转换成char型?
- 高手请进,在线安装 思路??
- 如何清除JTable中的所有数据?
- 问一个巨菜的问题,高分
- JDBC Driver连接sql server时,出现“Error establishing socket.”的问题
- System类中的方法
boolean suc =f.delete();
if (!suc) fail("文件删除失败");
这段语句应该放在
if (f.isDirectory()) {
}
外面
我用你的程序做了一下测试,没问题呀
注意在控制台输入java file a.txt时要在file.class所在的目录下