JDK1.7新特性nio提供了很多便捷的io文件访问方法,如Files工具类,其中的walkFileTree方法便可方法任何路径,有兴趣的朋友可以自行查看源码进行研究,以下是个人封装代码:
文件访问入口代码:
package com.xxx.cap.bm.metadata.file.tree.facade; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.UUID; import com.xxx.cap.bm.metadata.base.facade.CapBmBaseFacade; import com.xxx.cap.bm.metadata.file.tree.model.FileTreeVO; import com.xxx.cap.bm.metadata.file.tree.visitor.SingleLevelDirectoryVisitor; import com.xxx.cap.common.util.CollectionUtils; import com.xxx.cip.json.JSON; import xxx.org.directwebremoting.annotations.DwrProxy; import xxx.org.directwebremoting.annotations.RemoteMethod; /** * 文件树Facade * * @author 许畅 * @since JDK1.7 * @version 2017年4月27日 许畅 新建 */ @DwrProxy public class FileTreeFacade extends CapBmBaseFacade { /** 根目录名称"我的电脑" */ private static final String ROOT_NAME = "\u6211\u7684\u7535\u8111"; /** * 查询下一级所有文件夹 * * @param currentDir 当前文件夹 * @return 下一级所有文件 * @throws IOException IO异常 */ @RemoteMethod public FileTreeVO queryNextDirectionary(FileTreeVO currentDir) throws IOException { // 如果子集有数据则返回 if (CollectionUtils.isNotEmpty(currentDir.getChildren())) { return currentDir; } Path start = Paths.get(currentDir.getPath()); SingleLevelDirectoryVisitor visitor = new SingleLevelDirectoryVisitor(start); Files.walkFileTree(start, visitor); List<Path> results = visitor.getSingleResult(); List<FileTreeVO> nextResults = new ArrayList<FileTreeVO>(); for (Path path : results) { FileTreeVO next = new FileTreeVO(); next.setId(getRandomUUID()); next.setParentId(currentDir.getId()); next.setPath((path + "").replace("\\", "/")); next.setName(path.getFileName() + ""); nextResults.add(next); } currentDir.setChildren(nextResults); return currentDir; } /** * 查询个人电脑上的磁盘树 * * @return 电脑上的磁盘 */ @RemoteMethod public FileTreeVO queryComputerDisks() { FileTreeVO root = new FileTreeVO(); root.setId(getRandomUUID()); root.setName(ROOT_NAME); root.setPath(""); root.setParentId("-1");// 根目录 root.setExpand(true); List<FileTreeVO> computerDisks = new ArrayList<FileTreeVO>(); for (File disk : File.listRoots()) { FileTreeVO fileTreeVO = new FileTreeVO(); fileTreeVO.setPath(disk.getPath().replace("\\", "/")); fileTreeVO.setName(disk.getPath().replace("\\", "/")); fileTreeVO.setId(getRandomUUID()); fileTreeVO.setParentId(root.getId()); fileTreeVO.setExpand(true); computerDisks.add(fileTreeVO); } root.setChildren(computerDisks); return root; } /** * @param args 测试 * @throws IOException xx */ public static void main(String[] args) throws IOException { FileTreeFacade f = new FileTreeFacade(); FileTreeVO start = new FileTreeVO(); start.setId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase()); start.setName("D:/"); start.setPath("D:/"); start.setParentId("-1"); f.queryNextDirectionary(start); System.out.println("我的电脑:" + JSON.toJSONString(f.queryComputerDisks())); System.out.println("D盘:" + JSON.toJSONString(start)); } /** * @return 获取UUID */ public String getRandomUUID() { return UUID.randomUUID().toString().replaceAll("-", "").toLowerCase(); } }
package com.xxx.cap.bm.metadata.file.tree.visitor; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; /** * 单个层次文件夹访问器 * * @author 许畅 * @since JDK1.7 * @version 2017年4月27日 许畅 新建 */ public class SingleLevelDirectoryVisitor extends SimpleFileVisitor<Path> { /** 当前文件路径 */ private final Path currentPath; /** 当前层次 */ private final List<Path> singleResult = new ArrayList<Path>(); /** 级别 */ private final int level; /** * 构造方法 * * @param currentPath 当前文件 */ public SingleLevelDirectoryVisitor(Path currentPath) { this.currentPath = currentPath; this.level = 1; } /** * 构造方法 * * @param currentPath 当前文件 * @param level 级别 */ public SingleLevelDirectoryVisitor(Path currentPath, int level) { this.currentPath = currentPath; this.level = level; } /** * 只请求一个层次的文件夹 * * @param dir path * @param exc 异常 * @return FileVisitResult * @throws IOException IOException * * @see java.nio.file.SimpleFileVisitor#postVisitDirectory(java.lang.Object, java.io.IOException) */ @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if ((currentPath.getNameCount() + level) == dir.getNameCount()) { singleResult.add(dir); return FileVisitResult.CONTINUE; } return FileVisitResult.SKIP_SIBLINGS; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { return super.preVisitDirectory(dir, attrs); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { return super.visitFile(file, attrs); } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } /** * @return the currentPath 当前文件夹位置 */ public Path getCurrentPath() { return currentPath; } /** * @return the singleResult 获取文件夹结果集 */ public List<Path> getSingleResult() { return singleResult; } /** * @return the level 文件夹层次 */ public int getLevel() { return level; } }
package com.xxx.cap.bm.metadata.file.tree.model; import java.util.List; import xxx.org.directwebremoting.annotations.DataTransferObject; /** * 文件树模型 * * @author 许畅 * @since JDK1.7 * @version 2017年4月24日 许畅 新建 */ @DataTransferObject public class FileTreeVO extends TreeVO { /** id */ private String id; /** 父id */ private String parentId; /** 文件路径 */ private String path; /** 名称 */ private String name; /** 子文件集合 */ private List<FileTreeVO> children; /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the parentId */ public String getParentId() { return parentId; } /** * @param parentId the parentId to set */ public void setParentId(String parentId) { this.parentId = parentId; } /** * @return the path */ public String getPath() { return path; } /** * @param path the path to set */ public void setPath(String path) { setKey(path); this.path = path; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { setTitle(name); this.name = name; } /** * @return the children */ public List<FileTreeVO> getChildren() { return children; } /** * @param children the children to set */ public void setChildren(List<FileTreeVO> children) { this.children = children; } }
package com.xxx.cap.bm.metadata.file.tree.model; import com.xxx.cap.bm.metadata.base.model.BaseMetadata; /** * 基础树 * * @author 许畅 * @since JDK1.7 * @version 2017年4月28日 许畅 新建 */ public abstract class TreeVO extends BaseMetadata { /** * 构造方法 */ public TreeVO() { // set default value setIcon("image/folder.png"); } /** 标题 */ private String title; /** key */ private String key; /** 是否展开 */ private boolean expand; /** 是否文件夹 */ private boolean isFolder = true; /** 树节点显示图标 */ private String icon; /** * @return the title */ public String getTitle() { return title; } /** * @param title the title to set */ public void setTitle(String title) { this.title = title; } /** * @return the key */ public String getKey() { return key; } /** * @param key the key to set */ public void setKey(String key) { this.key = key; } /** * @return the expand */ public boolean isExpand() { return expand; } /** * @param expand the expand to set */ public void setExpand(boolean expand) { this.expand = expand; } /** * @return the isFolder */ public boolean isFolder() { return isFolder; } /** * @param isFolder the isFolder to set */ public void setFolder(boolean isFolder) { this.isFolder = isFolder; } /** * @return the icon 树节点显示图标 */ public String getIcon() { return icon; } /** * @param icon the icon to set 树节点显示图标 */ public void setIcon(String icon) { this.icon = icon; } }