在目录中搜索任何.XML文件 [英] Search directory for any .XML file

查看:61
本文介绍了在目录中搜索任何.XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使一段代码起作用.目的是检查特定目录中是否存在.XML文件.

I am trying to get a piece of code working. The aim is to check if there is an .XML file in a certain directory.

这是我到目前为止所掌握的.

This is what I've got so far.

File f = new File("saves/*.xml");

    if(f.exists()) {
        /* Do Something */
    } else {
        /* do something else */
    }

我正在尝试使用通配符搜索以.XML结尾的任何文件,我在这里缺少简单的东西吗?是否有一种更简单的方法来检查指定目录中是否存在至少一个.XML文件?

I'm trying to use a wildcard to search for any file ending in .XML, am I missing something simple here? Is there an easier way to check that at least one .XML file exists in a specified directory?

提前谢谢.

推荐答案

您可以使用此

    File dir = new File("saves");
    if (dir.isDirectory()) {
        File[] xmlFiles = dir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File folder, String name) {
                return name.toLowerCase().endsWith(".xml");
            }
        });
    }

现在您所有的xml文件都在File[] xmlFiles中.

Now all of your xml files are in the File[] xmlFiles.

这篇关于在目录中搜索任何.XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆