集册 Java实例教程 用通配符或逗号解析路径。

用通配符或逗号解析路径。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

595
用通配符或逗号解析路径。


import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.File;// from n o w j a v a . c o m - 时代Java

import java.io.FileFilter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.FilenameFilter;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.io.StringWriter;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.regex.Matcher;

import java.util.regex.Pattern;


public class Main{/*来自 N o w J a v a . c o m*/

    public static void main(String[] argv) throws Exception{

        String pWildcardPath = "nowjava.com";

        System.out.println(java.util.Arrays.toString(listFiles(pWildcardPath)));

    }

    /**

     * Resolve path with a wildcard or a comma.

     * example: file1,file2,file3 or xxx*.ext 

     */

    public static String[] listFiles(String pWildcardPath) {

        if (pWildcardPath.contains(",")) {

            String[] files = pWildcardPath.split(",");

            return files;

        }

        if (pWildcardPath.contains("*")) {


            return dirList(pWildcardPath);

        }

        return new String[] { pWildcardPath };

    }

    private static String[] dirList(String fname) {

        if (isWindows()) {

            fname = fname.replace('\\', '/');

        }

        String dirPath = "";

        if (fname.contains("/")) {

            int lPosFilename = fname.lastIndexOf("/");

            dirPath = fname.substring(0, lPosFilename + 1);

 
展开阅读全文