Java 检查 zip 文件的目录

发布于 2024-10-20 18:34:10 字数 973 浏览 3 评论 0原文

我有下面的代码,用于检查 SD 卡上是否存在文件夹,如果文件夹存在,我想添加另一个 if 语句,以检查实际文件夹中是否存在 zip 文件(如果确实存在)。我可以做什么来检查文件夹中的邮政编码扩展名。该文件夹中应该有很多 zip 文件,但我只想检查以确保有 zip 文件且没有其他文件扩展名。我感谢您对此提供的任何帮助。

File z = new File("/mnt/sdcard/folder");
if(!z.exists()) {
Toast.makeText(MainMethod.this, "/sdcard/folder Not Found!", Toast.LENGTH_LONG).show(); 
} else {
Toast.makeText(MainMethod.this, "/sdcard/folder Found!", Toast.LENGTH_LONG).show();
}

编辑: 感谢大家的帮助,这是我在你们的帮助下最终使用的,我还没有测试过,但它对我来说看起来不错。

    File z = new File("/mnt/sdcard/Folder");
    if(!z.exists()) {
           //create folder
} else {
        FilenameFilter f2 = new FilenameFilter() {
        public boolean accept(File dir, String filename) {
        return filename.endsWith("zip");
        }
        };
            if (z.list(f2).length > 0) {
            // there's a zip file in there..
            } else {
            //no zips inside folder
        }
    }

I have the below code which checks to see if folder exists on the sdcard, I would like to add another if statement if the folder exists to check that there are zip files inside the actual folder if it in fact exists. What could i do to check the folder for a zip extension. The folder should have a lot of zips in it but i only want it to check to make sure there are zips and no other file extension. I thank you for any help with this.

File z = new File("/mnt/sdcard/folder");
if(!z.exists()) {
Toast.makeText(MainMethod.this, "/sdcard/folder Not Found!", Toast.LENGTH_LONG).show(); 
} else {
Toast.makeText(MainMethod.this, "/sdcard/folder Found!", Toast.LENGTH_LONG).show();
}

EDIT:
Thanks guys for the help here is what i ended up using with your help, i haven't tested it yet but it looks good to me.

    File z = new File("/mnt/sdcard/Folder");
    if(!z.exists()) {
           //create folder
} else {
        FilenameFilter f2 = new FilenameFilter() {
        public boolean accept(File dir, String filename) {
        return filename.endsWith("zip");
        }
        };
            if (z.list(f2).length > 0) {
            // there's a zip file in there..
            } else {
            //no zips inside folder
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-10-27 18:34:10
File f = new File("folder");
FilenameFilter f2 = new FilenameFilter() {
public boolean accept(File dir, String filename) {
return filename.endsWith("zip");
}
};
if (f.list(f2).length > 0) {
// there's a zip file in there..
}

试试上面的..

File f = new File("folder");
FilenameFilter f2 = new FilenameFilter() {
public boolean accept(File dir, String filename) {
return filename.endsWith("zip");
}
};
if (f.list(f2).length > 0) {
// there's a zip file in there..
}

Try the above..

丑疤怪 2024-10-27 18:34:10

您是否看过 FileNameFilter

File f = new File("/mnt/sdcard/folder");
if(e.exist()){//file exist ??

File[] matchingFiles = f.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.endsWith("zip");
    }
});//list out files with zip at the end

}

Have you looked at FileNameFilter ?

File f = new File("/mnt/sdcard/folder");
if(e.exist()){//file exist ??

File[] matchingFiles = f.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.endsWith("zip");
    }
});//list out files with zip at the end

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文