当前位置:编程学习 > JAVA >>

文件删除时怎么支持通配符?? Java

public Boolean DeleteFile() {
        if (sSMBSourceFile == null) {
            File fFile = new File(sSourceFile);
            if (fFile.isFile()) {
                /* Set the file , can be deleted */
                if (!fFile.canWrite()) {
                    fFile.setWritable(true);
                }
                return fFile.delete();
            } else if (fFile.isDirectory()) {
                return pDeleteDirectory(sSourceFile);
            } else {
                return true;
            }
        } else {
            if (sourceExist) {
                return deleteSMBFile(sSMBSourceFile);
            } else {
                return true;
            }
        }
    }
现在是这个方法,怎么改动,然后支持通配符操作。、求大神。。参考下面的代码。
 public void deleteSigns(String sSrcPath) {
        File file = new File(sSrcPath.substring(0, sSrcPath.lastIndexOf("/")));
        File[] fileList = file.listFiles();
        if (fileList != null) {
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isFile()) {
                    String sTmpSrc = fileList[i].getAbsolutePath().replace("\\", "/");
                    String fileName = fileList[i].getName();
                    String fileFirstName = fileName;
                    String fileLastName = "";
                    if (fileName.lastIndexOf(".") != -1) {
                        fileFirstName = fileName.substring(0, fileName.lastIndexOf("."));
                        if (fileName.length() > fileName.lastIndexOf(".") + 1) {
                            fileLastName = fileName.substring(fileName.lastIndexOf(".") + 1);
                        }
                    }
                    if (sTPF.equals("*") && !sTPF2.equals("*")) {
                        if (!sTPF2.equals(fileLastName.toUpperCase())) {
                            continue;
                        }
                    }
                    if (!sTPF.equals("*") && sTPF2.equals("*")) {
                        if (!sTPF2.equals(fileFirstName.toUpperCase())) {
                            continue;
                        }
                    }
                    if (这个方法怎么写?) {
                        AddLog(EnumLogType.LOGTYPE_ERROR, EnumBuildStatus.BSTATUS_ONGOING, "Del failed from [" + sTmpSrc + "] ");
                        bTotalRlt = false;
                        break;
                    } else {
                        bTotalRlt = true;
                    }
                }
            }
        }

Java 通配符 --------------------编程问答-------------------- 大神都下班了吗?? --------------------编程问答-------------------- startWith  endWith  contacts都能解决 --------------------编程问答--------------------
引用 2 楼 fangmingshijie 的回复:
startWith  endWith  contacts都能解决

我也明白有好多种方法,可是我刚做维护,还是新手,怕乱搞会弄乱了,下面那个方法是让我照着写的、、 --------------------编程问答-------------------- 只考虑三种情况就好,*.*  *. 和.*  --------------------编程问答-------------------- 很方便
假设命令行传来的参数叫wild。

用正则
String regex = "^" + wild.replace(".", "\\.").replace("*", ".*").replace("?", ".?") + "$";

判断时候
if (file.getName().matches(regex)) {
...
}
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,