Java 检查 zip 文件的目录
我有下面的代码,用于检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试上面的..
Try the above..
您是否看过
FileNameFilter
?Have you looked at
FileNameFilter
?