能不能帮我解决下这个题谢谢

warword0 2008-11-24 01:19:20
就是打印一个目录下的所有子目录和文件(递归,树型)
我写到这里,不知道怎么让它成树型了,请各位帮帮忙吧!
package com.mjn.file;

import java.io.*;

public class PrintTree {

public static void main(String[] args) {

File f = new File("main");

System.out.println(f.getName());

printTree(f,0);

}

public static void printTree(File f,int level) {

String pre = " ";
level++;
pre+=" ";

if(f.isDirectory()) {

File[] f1 = f.listFiles();

for(int i = 0;i < f1.length;i++) {

System.out.println(pre+f1[i].getName());
printTree(f1[i],level);
}
}
}
}
...全文
124 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
warword0 2008-11-24
  • 打赏
  • 举报
回复
谢谢四楼的朋友了!!!
shenjie1989 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 possibleonline 的回复:]
package org.leelin.demo;

import java.io.File;

public class PrintTree {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=1)
{
System.out.println("输入路径不对!");
System.exit(0);
}
String pathName=args[0];
File file=new File(pathName);
if(!file.exists())
{
System.out.println("文件或目录不存在!"…
[/Quote]
4楼正确
upgrade_007 2008-11-24
  • 打赏
  • 举报
回复
up ding
ryb_cool 2008-11-24
  • 打赏
  • 举报
回复
随便看看,UP
shuaiAWP 2008-11-24
  • 打赏
  • 举报
回复
4楼正解啊
possibleonline 2008-11-24
  • 打赏
  • 举报
回复
package org.leelin.demo;

import java.io.File;

public class PrintTree {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length!=1)
{
System.out.println("输入路径不对!");
System.exit(0);
}
String pathName=args[0];
File file=new File(pathName);
if(!file.exists())
{
System.out.println("文件或目录不存在!");
System.exit(0);
}
printDirTree(file,0);
}

private static void printDirTree(File file,int level) {
String str="";
for(int i=0;i<level;i++)
str=str+" ";
if(file.isDirectory()){
File[] f=file.listFiles();
System.out.println(str+file.getName()+"("+f.length+")");
for(int i=0;i<f.length;i++){
printDirTree(f[i], level+1);
}
}else{
System.out.println(str+file.getName());
}
}

}
wind1373290 2008-11-24
  • 打赏
  • 举报
回复

package com.mjn.file;

import java.io.*;

public class PrintTree {

public static void main(String[] args) {

File f = new File("main");

System.out.println(f.getName());

String str = " ";

printTree(f,str);

}

public static void printTree(File f,String str) {

if(f.isDirectory()) {

File[] f1 = f.listFiles();

for(int i = 0;i < f1.length;i++) {

System.out.println(str+f1[i].getName());
printTree(f1[i],str+" ");
}
}
}
}
warword0 2008-11-24
  • 打赏
  • 举报
回复
谢谢了,但是能不能用递归呢?
wind1373290 2008-11-24
  • 打赏
  • 举报
回复
我用的是队列 希望你能看的懂

package com.mjn.file;
import java.io.*;
import java.util.*;
public PrintTree {
public static void main(String[] args) {
File f = new File("main");
printTree(f);
}
public static void printTree(File f) {
String str=" ";
List list = new LinkedList();
list.add(f);
while(list.size()!=0) {
File f1 = (File)list.remove(0);
System.out.println(str+f1.getName());
if(f1.isDirectory()) {
File[] f2 = f1.listFiles();
for(int i=0;i<f2.length;i++)
list.add(f2[i]);
str+=" ";
}
}
}
}

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧