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;
}
}
其中在file.listfile时候,可以加一些过滤条件
// 如果存在 就获取包下的所有文件 包括目录
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
public boolean accept(File file) {
return (recursive && file.isDirectory())
|| (file.getName().endsWith(".class"));
}
});