JAVA文件目录遍历缩进算法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA文件目录遍历缩进算法相关的知识,希望对你有一定的参考价值。
遍历某个文件夹,将文件夹下的所有文件都遍历出来。而且要求要有类似资源管理器的树形结构。
目录是遍历出来了,但树形结构的缩进算法想不出来。有哪位高人帮帮忙~~~
目录遍历采用的是递归算法。。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileFilter;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.*;
import javax.swing.tree.*;
public class MainFrame extends JFrame
private JScrollPane imagesp = new JScrollPane();
private JTree tree = new JTree(FileTreeNode.ROOT);
private TreePath lastFilePath;
private JButton scaleB = new JButton("放大");
private JButton scaleS = new JButton("缩小");
private Image img;
private float beishu = 1;
public static void main(String[] args)
new MainFrame();
public MainFrame ()
scaleB.setEnabled(false);
scaleS.setEnabled(false);
this.setSize(400,400);
this.setLayout(new BorderLayout());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(1,2));
JScrollPane treesp = new JScrollPane();
treesp.setViewportView(tree);
centerPanel.add(treesp);
tree.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent event)
TreePath treePath = tree.getSelectionPath();
if(treePath!=lastFilePath)
FileTreeNode node = (FileTreeNode)treePath.getLastPathComponent();
if(node.isFile())
String fpath = node.getPath();
lastFilePath = treePath;
img = Toolkit.getDefaultToolkit().createImage(fpath);
imagesp.setViewportView(new JLabel(new ImageIcon(img)));
beishu = 1;
imagesp.repaint();
scaleB.setEnabled(true);
scaleS.setEnabled(true);
);
centerPanel.add(imagesp);
this.add(centerPanel,BorderLayout.CENTER);
JPanel btnPanel = new JPanel();
btnPanel.add(scaleB);
btnPanel.add(scaleS);
scaleB.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
beishu*=2;
Image scaleIMG = img.getScaledInstance((int)(img.getWidth(imagesp)*beishu), (int)(img.getHeight(imagesp)*beishu), Image.SCALE_DEFAULT);
imagesp.setViewportView(new JLabel(new ImageIcon(scaleIMG)));
imagesp.repaint();
);
scaleS.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
beishu/=2;
Image scaleIMG = img.getScaledInstance((int)(img.getWidth(imagesp)*beishu), (int)(img.getHeight(imagesp)*beishu), Image.SCALE_DEFAULT);
imagesp.setViewportView(new JLabel(new ImageIcon(scaleIMG)));
imagesp.repaint();
);
this.add(btnPanel,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
class FileTreeNode implements TreeNode
private String name;
private String path;
private boolean isFile;
private Vector<FileTreeNode> children;
private FileTreeNode parent;
private final static ImageFileFilter filter = new ImageFileFilter();
public final static FileTreeNode ROOT = new FileTreeNode("我的电脑","",null);
private FileTreeNode(String name, String path, FileTreeNode parent)
this.name = name;
this.path = path;
this.parent = parent;
File f = new File(path);
if(f.isFile())isFile = true;
else isFile = false;
public Enumeration<FileTreeNode> children()
if(children==null)initChild();
return children.elements();
public String getName()
return name;
public String getPath()
return path;
public boolean isFile()
return isFile;
public boolean getAllowsChildren()
return !isFile;
public TreeNode getChildAt(int childIndex)
if(children==null)initChild();
return children.elementAt(childIndex);
public int getChildCount()
if(children==null)initChild();
return children.size();
public int getIndex(TreeNode node)
return children.indexOf(node);
public TreeNode getParent()
return parent;
public boolean isLeaf()
return isFile;
public String toString()
return name;
private void initChild()
if(this==ROOT)
children = new Vector<FileTreeNode>();
File[] files = File.listRoots();
for(int i=0;i<files.length;i++)
FileTreeNode child = new FileTreeNode(files[i].getPath(),files[i].getPath(),this);
children.add(child);
else
children = new Vector<FileTreeNode>();
File current = new File(path);
if(current.isDirectory())
File[] files = current.listFiles(filter);//filefilter to add
for(int i=0;i<files.length;i++)
FileTreeNode child = new FileTreeNode(files[i].getName(),files[i].getPath(),this);
children.add(child);
class ImageFileFilter implements FileFilter
public boolean accept(File pathname)
if(pathname.isDirectory())return true;
else
String name = pathname.getName();
return match(name);
private boolean match(String name)
if(name.endsWith("jpg"))return true;
else if(name.endsWith("gif"))return true;
else if(name.endsWith("bmp"))return true;
return false;
其实是给另一个问题写的,不过别个已经结了。损失好多分呀。
现在只显示3种文件类型,把FileFilter去掉就行了。 参考技术A 那要看你的程序怎么写得改改就可以了。
你可以用一个Sting存储缩进的空格或者用int存储缩进的数量。进入一个目录增加一位空格就可以了 参考技术B public static void main(String[] args)
File file = new File("C:" + File.separator + "aa");
f(file,0);
public static void f(File file,int level)
if(!file.exists())return;
String str="";
for(int i=0;i<level;i++)
str=str+" ";
System.out.println(str+file.getName());
if (file.isFile())return;
if (file.isDirectory())
File[] files = file.listFiles();
for (int n = 0; n < files.length; n++)
f(files[n],level+1);
路径自己修改
java_遍历文件目录
package util; import java.io.File; import java.io.IOException; //列出File的一些常用操作 public class util { /* * 列出指定目录下(包括其子目录)的所有文件 */ public static void listDirectory(File dir)throws IOException { if(!dir.exists()) throw new IllegalArgumentException("目录:"+dir+"不存在."); if(!dir.isDirectory()){ throw new IllegalArgumentException(dir+"不是目录。"); } /*String[] filenames=dir.list();//返回的是字符串数组 直接子的名称 不包含子目录 for(String string:filenames){ System.out.println(dir+"\\"+string); }*/ //如果要遍历子目录下的内容就需要构造File对象做递归操作,File提供了直接返回File对象的API File[] files=dir.listFiles(); //for(File file:files){ //System.out.println(file); if(files!=null&&files.length>0){ for(File file:files){ if(file.isDirectory()) //递归 listDirectory(file); else System.out.println(file); } } } }
package util; import java.io.File; import java.io.IOException; public class test { public static void main(String[] args)throws IOException { util.listDirectory(new File("D:\\一些工具")); } }
以上是关于JAVA文件目录遍历缩进算法的主要内容,如果未能解决你的问题,请参考以下文章
java 如何递归遍历多重目录下的指定格式文件复制到目标目录并改格式