欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

快速找出Android SD卡中的文件,使用实用正则表达式扫描

最编程 2024-01-25 11:18:39
...
package match;
 
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Utils {
        /**
          * 遍历指定文件夹下的资源文件
          * @param folder 文件
          */
        public static void simpleScanning(File folder) {
                //指定正则表达式
                Pattern mPattern = Pattern.compile( "([^\\.]*)\\.([^\\.]*)" );
                // 当前目录下的所有文件
                final String[] filenames = folder.list();
                // 当前目录的名称
                //final String folderName = folder.getName();
                // 当前目录的绝对路径
                //final String folderPath = folder.getAbsolutePath();
                if (filenames != null ) {
                        // 遍历当前目录下的所有文件
                        for (String name : filenames) {
                                File file = new File(folder, name);
                                // 如果是文件夹则继续递归当前方法
                                if (file.isDirectory()) {
                                        simpleScanning(file);
                                }
                                // 如果是文件则对文件进行相关操作
                                else {
                                        Matcher matcher = mPattern.matcher(name);
                                        if (matcher.matches()) {
                                                // 文件名称
                                                String fileName = matcher.group( 1 );
                                                // 文件后缀
                                                String fileExtension = matcher.group( 2 );
                                                // 文件路径
                                                String filePath = file.getAbsolutePath();
                                                 
                                                if (Utils.isMusic(fileExtension)) {
                                                        // 初始化音乐文件......................
                                                        System.out.println( "This file is Music File,fileName=" +fileName+ "."
                                                                        +fileExtension+ ",filePath=" +filePath);
                                                }
                                                 
                                                if (Utils.isPhoto(fileExtension)) {
                                                        // 初始化图片文件......................
                                                        System.out.println( "This file is Photo File,fileName=" +fileName+ "."
                                                                        +fileExtension+ ",filePath=" +filePath);
                                                }
                                                 
                                                if (Utils.isVideo(fileExtension)) {
                                                        // 初始化视频文件......................
                                                        System.out.println( "This file is Video File,fileName=" +fileName+ "."
                                                                        +fileExtension+ ",filePath=" +filePath);
                                                }
                                        }
                                }
                        }
                }
        }
        /**
          * 判断是否是音乐文件
          * @param extension 后缀名
          * @return
          */
        public static boolean isMusic(String extension) {