J2ME FileConnection 删除整个目录

最近一个项目里用到了JSR75-FileConnection,第一次用某个API总会出不少问题,
这里记录下来,同时也和大家分享一下。

本文问题:删除目录的时候是用FileConnection的delete()函数,发现对目录只有空的才能删除,
否则会报java.io.IOException。没办法,只能自己写一个删除函数了,目录、文件通统杀掉。
void delete() Deletes the file or directory specified in the Connector.open() URL.

/*
* function: 删除文件或者目录,如果是目录,则删除目录下所有子目录和文件.
* param: String name -- 要删除的文件或者目录全路径,比如: "file:///root1/testdir/"
* auther: smilelance
*/

private void deleteDirFiles(String name){
print("to be deleted: " + name);
FileConnection fc = null;
try {
fc=(FileConnection)Connector.open(name);
if(fc.exists()){
if(!fc.canWrite()){
print("file is read only.");
fc.setReadable(true);
// if(!fc.canWrite()){
// print("read only after set");
// }
}
if(fc.isDirectory()){
Enumeration e = fc.list();
while(e.hasMoreElements()){
//System.out.println(e.nextElement());
deleteDirFiles(name + e.nextElement());
}
}
fc.delete();
}else{
print("file don't exsit!");
}
}catch (Exception e) {
System.out.println( "Error: " + e.toString());
} finally {
try {
fc.close();
} catch(Exception e) {
System.out.println(e.toString());
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值