递归获取某个路径下的所有文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29956725/article/details/86677409
package spring.agent;

import java.io.File;

import org.junit.Test;

public class FileNameTest {

    private final String host = "http://localhost:7081/wcl-web/";
    private final String rootPath = "D:/MyWorkSpacePractice/ACL/wcl-web/src/main/webapp/WEB-INF/pages";

    @Test
    public void getFileName() {
        File file = new File(rootPath);
        getFileName(file);
    }

    public void getFileName(File file) {

        File[] listFiles = file.listFiles();
        if (file.isDirectory() && listFiles.length > 0) {
            for (int i = 0; i < listFiles.length; i++) {
                getFileName(listFiles[i]);
            }
        } else {
            System.out.println(getFileParentPath(file));
            makeUrl(getFileParentPath(file));
        }
    }

    public void makeUrl(String absolutePath) {
        String httpUrl = absolutePath.replace("\\", "/").replace(rootPath + "/", host + "test/")
                .replace("/final/", "/1/").replace("/includes/", "/0/").replace(".ftl", "");
        if (!(httpUrl.contains("/test/1") || httpUrl.contains("/test/0"))) {
            httpUrl = httpUrl.replace("/test/", "/test/3/");
        }
        System.out.println(httpUrl);
        // absolutePath.startsWith("")
    }

    public String getFileParentPath(File file) {
        File filePath = new File(file.getAbsolutePath());

        String path = filePath.getAbsolutePath();

        while (path.equals(rootPath)) {
            File fileLoop = filePath.getParentFile();
            path = fileLoop.getAbsolutePath();
        }
        return path;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_29956725/article/details/86677409