展开全部
为了避免目录列举消耗时间过长,请指定一个目录来模拟,命令行参数:代表路径的字符串.
如果认可代码,请加分50,谢谢
----
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.io.*;
final public class FileTree extends JFrame {
public FileTree(File dir) throws HeadlessException {
super("File Tree");
JTree tree;
add(new JScrollPane(tree =new JTree(buildTreeModel(dir))));
tree.setCellRenderer(new FileTreeRenderer());
setSize(400,600);
setVisible(true);
}
private TreeModel buildTreeModel(File dir){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir);
walkthrough(dir,root);
return new DefaultTreeModel(root);
}
private static void walkthrough(File f,DefaultMutableTreeNode node){
for (File fle : f.listFiles()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(fle);
node.add(n);
if (fle.isDirectory()){
walkthrough(fle, n);
}
}
}
private class FileTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel cmp = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)value;
Object obj = n.getUserObject();
if (obj instanceof File) {
File f = (File)obj;
cmp.setText(f.getName());
cmp.setForeground(f.isDirectory()?Color.BLUE:Color.BLACK);
}
}
return cmp;
}
}
public static void main(String[] args) {
new FileTree(new File(args[0]));
}
}
如果认可代码,请加分50,谢谢
----
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
import java.io.*;
final public class FileTree extends JFrame {
public FileTree(File dir) throws HeadlessException {
super("File Tree");
JTree tree;
add(new JScrollPane(tree =new JTree(buildTreeModel(dir))));
tree.setCellRenderer(new FileTreeRenderer());
setSize(400,600);
setVisible(true);
}
private TreeModel buildTreeModel(File dir){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(dir);
walkthrough(dir,root);
return new DefaultTreeModel(root);
}
private static void walkthrough(File f,DefaultMutableTreeNode node){
for (File fle : f.listFiles()) {
DefaultMutableTreeNode n = new DefaultMutableTreeNode(fle);
node.add(n);
if (fle.isDirectory()){
walkthrough(fle, n);
}
}
}
private class FileTreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
JLabel cmp = (JLabel)super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
if (value instanceof DefaultMutableTreeNode) {
DefaultMutableTreeNode n = (DefaultMutableTreeNode)value;
Object obj = n.getUserObject();
if (obj instanceof File) {
File f = (File)obj;
cmp.setText(f.getName());
cmp.setForeground(f.isDirectory()?Color.BLUE:Color.BLACK);
}
}
return cmp;
}
}
public static void main(String[] args) {
new FileTree(new File(args[0]));
}
}
展开全部
何必如此复杂
import java.io.*;
public class ListFile{
static public void main(String[] str)throws Exception{
File f=null;
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个目录:");
String path=read.readLine();
f=new File(path);
File[] list=f.listFiles();
for(int i=0;i<list.length;i++)
System.out.println(list[i]);
}
}
import java.io.*;
public class ListFile{
static public void main(String[] str)throws Exception{
File f=null;
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个目录:");
String path=read.readLine();
f=new File(path);
File[] list=f.listFiles();
for(int i=0;i<list.length;i++)
System.out.println(list[i]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2008-11-26
展开全部
//何必如此复杂,分不要给我,给我楼上的朋友
//yuge4725 简直画蛇添足!太搞笑了
import java.io.*;
public class ListFile{
static public void main(String[] str)throws Exception{
File f=null;
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个目录:");
String path=read.readLine();
f=new File(path);
File[] list=f.listFiles();
for(int i=0;i<list.length;i++)
System.out.println(list[i].getAbsolutePath());//打印全路径
}
}
//yuge4725 简直画蛇添足!太搞笑了
import java.io.*;
public class ListFile{
static public void main(String[] str)throws Exception{
File f=null;
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入一个目录:");
String path=read.readLine();
f=new File(path);
File[] list=f.listFiles();
for(int i=0;i<list.length;i++)
System.out.println(list[i].getAbsolutePath());//打印全路径
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询