public Object clone() throws CloneNotSupportedException{ return super.clone(); } }
有那么麻烦吗?public static void main(String[] args) { File f = new File("C:/Intel"); StringBuffer prefix = new StringBuffer(""); System.out.println(f.getName()); list(f, prefix); } public static void list(File path, StringBuffer prefix) { if (path != null && path.exists() && path.isDirectory()) { prefix.append("\t"); File[] childs = path.listFiles(); if (childs != null && childs.length > 0) { for (File child : childs) { System.out.println(prefix + "|--" + child.getName()); list(child, prefix); } } prefix.deleteCharAt(prefix.length()-1); } }
除了盘符没有打印,其它都打印了import java.io.*;public class SearchTree { public static void main(String[] args) { search(new File("c:\\"), 0); }
public static void search(File file, int n) { if (file.isDirectory()) { for (int i = 0; i < n; i++) { System.out.print("\t|"); } System.out.println("--" + file.getName()); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { search(files[i], n + 1); } } } }
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;import org.mtmk.pathout.output.OutputProcessor;public class PathLoader {
private List<String> output = new ArrayList<String>();
private HierarchieList hirerarchialDesc = new HierarchieList();
private FileFilter filter = null;
private Comparator<File> comparator = null;
public void loadPath(File nodes){
record(nodes);
if(nodes.isFile()){
return;
}else{
File[] childs = filteFiles(nodes);
sortFiles(childs);
for(int idx=0;idx<childs.length;idx++){
hirerarchialDesc.add(childs.length > 1,childs[idx].isFile(),idx==childs.length-1);
loadPath(childs[idx]);
hirerarchialDesc.pop();
}
}
}
// 过滤节点
private File[] filteFiles(File childs) {
return childs.listFiles(filter);
} // 记录节点
private void record(File f){
String size =f.isFile()? ("("+ f.length()+")"):"";
output.add(hirerarchialDesc.getHierarchieString()+f.getName() + size);
}
// 排序节点
private void sortFiles(File[] files){
if(this.comparator != null)
Arrays.sort(files, this.comparator);
}
// 输出
public void output(OutputProcessor<String> outputer){
outputer.perform(output);
}
// 设置过滤器
public void setFilter(FileFilter filter){
this.filter = filter;
}
// 设置排序器
public void setComparator(Comparator<File> comparator){
this.comparator = comparator;
}}
package org.mtmk.pathout.pathout;import java.util.ArrayList;
import java.util.List;public class HierarchieList {
private List<Hierarchie> list = new ArrayList<Hierarchie>();
private boolean changed = false;
private StringBuilder output = new StringBuilder("");
private String strNode = "|------";
private String strNode2 = " ";
private String strNode3 = "+------";
private String strNode4 = "| ";
public String getHierarchieString() {
if (list.size() == 0) {
return "";
}
if (changed == false) {
return output.toString();
}
output.delete(0, output.length());
if(list.size()!=1){
for (int idx = 0; idx < list.size()-1; idx++) {
Hierarchie h = list.get(idx);
output.append((h.hasMultiFile() && !h.isLast()) ? strNode4 : strNode2);
}
}
Hierarchie lastH = list.get(list.size()-1);
output.append(lastH.isFile() ? strNode : strNode3);
return output.toString();
} public void add(boolean multi,boolean isFile,boolean isLast) {
changed = true;
list.add(new Hierarchie(multi,isFile,isLast));
} public void pop() {
changed = true;
list.remove(list.size() - 1);
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
{
File f = new File("C:/Intel");
StringBuffer prefix = new StringBuffer("");
System.out.println(f.getName());
list(f, prefix);
} public static void list(File path, StringBuffer prefix)
{
if (path != null && path.exists() && path.isDirectory())
{
prefix.append("\t");
File[] childs = path.listFiles();
if (childs != null && childs.length > 0)
{
for (File child : childs)
{
System.out.println(prefix + "|--" + child.getName());
list(child, prefix);
}
}
prefix.deleteCharAt(prefix.length()-1);
}
}
{
public static void main(String[] args)
{
search(new File("c:\\"), 0);
}
public static void search(File file, int n)
{
if (file.isDirectory())
{
for (int i = 0; i < n; i++)
{
System.out.print("\t|");
}
System.out.println("--" + file.getName());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
search(files[i], n + 1);
}
}
}
}
另外你可以参考tree命令,节点最后一个目录后面的连线也是不能打出来的。
public static void main(String[] args) {
File f=new File("c:\\");
//File[] fs=f.listFiles();
//for(File ff:fs){
//System.out.println(ff.getAbsolutePath());
//}
showFile(f,1); }
//遞歸輸出所有的目錄
public static void showFile(File f,int length){
StringBuffer sb=new StringBuffer("");
for(int i=0;i<length;i++){
sb.append("==");
}
if(f.isFile()){
//System.out.println(f.getName());
}else{
//是一個目錄
File[] fs=f.listFiles();//取出文件的子目錄
for(File ff:fs){
System.out.println(sb.toString()+ff.getName());
showFile(ff,length+1);
}
}
}}
import java.util.Stack;public class FileTreeTest {
public static void main(String[] args) {
FileTree tree = new FileTree("D:/green/apache/apache-tomcat-5.5.12/server");
tree.tree();
}
}class FileTree {
private File root;
public FileTree(File root) {
this.root = root;
}
public FileTree(String rootName) {
this(new File(rootName));
}
public void tree() {
Stack<Boolean> lastStack = new Stack<Boolean>();
tree(root, 0, lastStack);
} private void tree(File dir, int level, Stack<Boolean> lastStack) {
if(dir == null || !dir.exists()) {
return;
}
printFile(dir, level, lastStack);
if(dir.isFile()) {
return;
}
File[] children = dir.listFiles();
for(int i = 0; i < children.length; i++) {
lastStack.push(i == children.length - 1);
tree(children[i], level + 1, lastStack);
lastStack.pop();
}
}
private void printFile(File file, int level, Stack<Boolean> lastStack) {
for(int i = 0, k = lastStack.size() - 1; i < k; i++) {
System.out.print(lastStack.get(i) ? " " : "│ ");
}
if(level > 0) {
System.out.print(lastStack.get(lastStack.size() - 1) ? "└ " : "├ ");
}
System.out.println(level == 0 ? file.getPath() : file.getName());
}
}D:\green\apache\apache-tomcat-5.5.12\server
├ classes
├ lib
│ ├ catalina-ant-jmx.jar
│ ├ catalina-ant.jar
│ ├ catalina-cluster.jar
│ ├ catalina-optional.jar
│ ├ catalina-storeconfig.jar
│ ├ catalina.jar
│ ├ commons-modeler.jar
│ ├ servlets-cgi.renametojar
│ ├ servlets-default.jar
│ ├ servlets-invoker.jar
│ ├ servlets-ssi.renametojar
│ ├ servlets-webdav.jar
│ ├ tomcat-ajp.jar
│ ├ tomcat-apr.jar
│ ├ tomcat-coyote.jar
│ ├ tomcat-http.jar
│ └ tomcat-util.jar
└ webapps
├ host-manager
│ ├ host-manager.xml
│ ├ images
│ │ ├ add.gif
│ │ ├ code.gif
│ │ ├ design.gif
│ │ ├ docs.gif
│ │ ├ fix.gif
│ │ ├ jakarta-logo.gif
│ │ ├ tomcat.gif
│ │ ├ update.gif
│ │ └ void.gif
│ ├ manager.xml
│ └ WEB-INF
│ ├ lib
│ │ └ catalina-host-manager.jar
│ └ web.xml
└ manager
├ html-manager-howto.html
├ images
│ ├ add.gif
│ ├ code.gif
│ ├ design.gif
│ ├ docs.gif
│ ├ fix.gif
│ ├ jakarta-logo.gif
│ ├ tomcat.gif
│ ├ update.gif
│ └ void.gif
├ manager-howto.html
├ manager.xml
├ status.xsd
├ WEB-INF
│ ├ lib
│ │ ├ catalina-manager.jar
│ │ └ commons-fileupload-1.0.jar
│ └ web.xml
└ xform.xsl
doubanradio
+------chrome
| +------content
| | |------aalist.xul(1249)
| | |------controller.js(32834)
| | |------doubanradio.xul(2452)
| | |------doubanradio.xul~(2411)
| | |------findlyric.xul(8283)
| | |------findlyric.xul~(7987)
| | +------kit
| | | |------hookdouban.js(13579)
| | | |------id3tag.js(3352)
| | | |------move.js(541)
| | | |------param.js(3885)
| | | |------sha1.js(5617)
| | | |------test.html(2085)
| | | |------test.html~(2063)
| | | └------util.js(38841)
| | |------listeditor.xul(5596)
| | +------module_lyric
| | | |------lrc.js(7247)
| | | |------yahoo.js(2134)
| | | └------youdao.js(1791)
| | +------module_radio
| | | |------douban.js(12940)
| | | |------radio.js(14141)
| | | |------renren.js(6261)
| | | |------sina.js(6044)
| | | └------songbean.js(2220)
| | |------mylist.xul(4830)
| | |------releasenote.js(1616)
| | |------windowlyric.xul(7578)
| | └------worker.js(1524)
| +------locale
| | └------zh-CN
| | └------overlay.properties(8730)
| └------skin
| |------album.png(761)
| |------artist.png(702)
| |------busy.png(1847)
| |------chat.png(3253)
| |------close.png(878)
| |------delay.png(432)
| |------delay2.png(440)
| |------douban.png(630)
| |------download.png(646)
| |------home.png(574)
| |------like.png(655)
| |------like2.png(578)
| |------logo.png(3992)
| |------lyric.png(380)
| |------overlay.css(993)
| |------renren.jpg(1150)
| |------sina.png(3412)
| |------stop.png(724)
| └------trash.png(573)
|------chrome.manifest(260)
+------defaults
| └------preferences
| └------prefs.js(3368)
|------doubanradio.xpi(97365)
|------install.rdf(925)
└------pack.bat(433)
/**
* 遍历某个目录,生成目录树
*
* @author luochengor
* @date Jul 14, 2011
* @Email [email protected]
*/
public class CreatMenuTree { /**
* @param args
*/
public static void main(String[] args) {
String dir = "C:\\Program Files\\apache-tomcat-6.0.29\\webapps\\manager";
createMenuTree(dir, 1);
}
/**
* 遍历某个目录,生成目录树
*
* @param dir 需要遍历的目录
* @param dirNum 目录为第几级目录
*/
public static void createMenuTree(String dir, int dirNum) {
for (int i=1; i<dirNum; i++) {
System.out.print("| ");
}
File file = new File(dir);
if (file.exists()) {
if (dirNum == 1) {
System.out.println("|--" + dir);
} else {
System.out.println("|--" + file.getName());
}
if (file.isDirectory()) {
String[] files = file.list();
for (int i=0; i<files.length; i++) {
createMenuTree(dir + "\\" + files[i], dirNum+1);
}
}
} else {
System.out.println(dir + ":此文件或目录不存在!");
}
}
}结果:|--C:\Program Files\apache-tomcat-6.0.29\webapps\manager
| |--401.jsp
| |--images
| | |--add.gif
| | |--asf-logo.gif
| | |--code.gif
| | |--design.gif
| | |--docs.gif
| | |--fix.gif
| | |--tomcat.gif
| | |--update.gif
| | |--void.gif
| |--META-INF
| | |--context.xml
| |--status.xsd
| |--WEB-INF
| | |--jsp
| | | |--sessionDetail.jsp
| | | |--sessionsList.jsp
| | |--web.xml
| |--xform.xsl