Resolve path with a wildcard or a comma. - Java File Path IO

Java examples for File Path IO:File Path

Description

Resolve path with a wildcard or a comma.

Demo Code


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
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{
    public static void main(String[] argv) throws Exception{
        String pWildcardPath = "java2s.com";
        System.out.println(java.util.Arrays.toString(listFiles(pWildcardPath)));
    }// ww  w.jav  a2 s  .c om
    /**
     * 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);
            fname = fname.substring(lPosFilename + 1);
        }
        File dir = new File(dirPath);
        return dir.list(new WildCardFileFilter(fname));
    }
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0;
    }
}

Related Tutorials