获取指定文件夹下的所有文件的绝对路径名

版权声明:转载请注明出处 https://blog.csdn.net/github_37412255/article/details/80148581
package facvisual;

import java.io.*;
import java.nio.file.*;
import java.util.*;

import com.hankcs.hanlp.HanLP;

/**
 * The tool class: the encapsulation of common functions.
 * @author Long weibing
 * @since 2018.04.29
 */
public class Utils {

    public static void main(String[] args) throws Exception {
        String curPath = "C:\\Users\\Administrator\\Desktop\\README.txt";
        List<String> namesResult = Utils.allFiles(curPath);
//        List<String> namesResult = Utils.allFilesForSuffix("txt", curPath);
        System.out.println("The numbers of all files: " + namesResult.size());
        StringBuilder sb = new StringBuilder();
        for (String name : namesResult) {
            BufferedReader br = new BufferedReader(new FileReader(name));
            String curLine = null;
            while ((curLine = br.readLine()) != null) {
                sb.append(curLine);
            }
            String text = sb.toString();
//            System.out.println(text);

            List<String> keywordList = HanLP.extractKeyword(text, 10000);
            System.out.println(keywordList);

            br = null;
        }

    }

    /**
     * Collecting all files absolute pathname under 'path' path
     * <font color="red">(No directories are included)</font>. If no file exist, return hasn't a element list.
     * @param path Search path
     * @return List<String> A collection of String type
     */
    public static List<String> allFiles(String path) {
        // The collection of all files absolute pathname.
        List<String> output = new LinkedList<String>();
        // Collect temporary files and directories.
        Stack<File> stack = new Stack<File>();
        File file = new File(path);
        File[] files = file.listFiles();
        boolean isFile = !Files.isDirectory(Paths.get(path));
        if (files != null) {
            stack.push(Arrays.asList(files));
        }
        if (isFile) {
            stack.push(file);
        }
        while (!stack.isEmpty()) {
            File curFile = stack.pop();
            String curEleAbsPath = curFile.toString();
            // It's a directory
            boolean isDir = Files.isDirectory(Paths.get(curEleAbsPath));
            if (isDir) {
                List<File> temp = null;
                if (curFile.listFiles() != null) {
                    temp = Arrays.asList(curFile.listFiles());
                }
                if (temp != null) {
                    stack.push(temp);
                }                
            } else {
                // Collect a file.
                output.add(curEleAbsPath);
            }
        }
        return output;
    }

    /**
     * Collecting all files absolute pathname under 'path' path and for 'suffixName' suffix name. 
     * If no file exist, return hasn't a element list.
     * @param suffixName Suffix name
     * @param path Search path
     * @return List<String> A collection of String type
     */
    public static List<String> allFilesForSuffix(String suffixName, String path) {
        List<String> allFiles = Utils.allFiles("E:\\");
        List<String> output = new LinkedList<String>();
        for (String file : allFiles) {
            String[] s = file.split("\\.");
            boolean isSufName = (s != null && s.length > 0 && s[s.length - 1].equals(suffixName));
            if (isSufName) {
                output.add(file);
            }
        }
        return output;
    }
}

猜你喜欢

转载自blog.csdn.net/github_37412255/article/details/80148581