查看文件内容

今天心血来潮想写一个用JTREE显示文件夹的JAVA程序。但是因为对JTREE所有甚少,所以查了一些资料,便有了以下的程序。

这个东西好像有两个值得保存的地方,第一,如何利用TREEMODEL来更新JTREE组件,第二,便是如何遍历文件夹

Main类:

 package org.lee.file; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class Main { public static void main(String... args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ FileFrame ff=new FileFrame(); ff.pack(); ff.setVisible(true); ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } }

FileFrame类:

package org.lee.file; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.tree.*; public class FileFrame extends JFrame { /** * */ private static final long serialVersionUID = 10009L; /* * 类属性 */ private JLabel pathLabel=new JLabel("路径:"); private JTextField path=new JTextField(20); private JButton doBtn=new JButton("确定"); private DefaultMutableTreeNode root=new DefaultMutableTreeNode(".."); private DefaultTreeModel treeModel=new DefaultTreeModel(root); private JTree tree=new JTree(treeModel); private JTextArea mess=new JTextArea(10,20); /* * Constructor */ public FileFrame(){ //构造页面 JPanel p=new JPanel(); p.add(pathLabel); p.add(path); p.add(doBtn); add(p,BorderLayout.NORTH); tree.setShowsRootHandles(true); tree.setEditable(true); add(new JScrollPane(tree),BorderLayout.CENTER); mess.setFont(new Font("黑体",Font.BOLD,20)); add(new JScrollPane(mess),BorderLayout.SOUTH); init(); } /* * 添加事件方法 */ private void init(){ //按钮事件 doBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent evt){ if(path.getText()!="" && !path.getText().equals("")){ File rootFile=new File(path.getText()); DefaultMutableTreeNode newRoot=new DefaultMutableTreeNode(rootFile.getPath()); treeModel.setRoot(newRoot); parserFile(newRoot,rootFile); } } }); tree.addMouseListener(mouseListener); } /* * @param node 树的节点 * @param f 需遍历的文件 * 遍历文件夹 */ public void parserFile(DefaultMutableTreeNode node,File f){ if(f.isDirectory()){ DefaultMutableTreeNode newNode=null; if(f.getPath().equals(path.getText())){ newNode=node; } else{ newNode=new DefaultMutableTreeNode(f.getName()); node.add(newNode); } File[] files=f.listFiles(); for(File file : files){ parserFile(newNode,file); } } else{ node.add(new DefaultMutableTreeNode(f.getName())); } } /* * 重写树的事件监听器 */ MouseListener mouseListener=new MouseAdapter(){ @Override public void mouseClicked(MouseEvent evt){ if(evt.getClickCount()>=2){ //获取选中节点 TreePath selfPath=tree.getPathForLocation(evt.getX(), evt.getY()); if(selfPath==null) return; DefaultMutableTreeNode node=(DefaultMutableTreeNode)selfPath.getLastPathComponent(); //获取选中节点所对应的路径 String nodePath=getNodePath(node); if(!nodePath.equals(path.getText())){ nodePath=path.getText()+"//"+getNodePath(node); } mess.setText(""); //文件内容类 FileContent fc=new FileContent(new File(nodePath)); mess.append(fc.toString()); } } }; /* * 得到节点所对应的文件路径 */ public String getNodePath(DefaultMutableTreeNode node){ DefaultMutableTreeNode newNode=node; DefaultMutableTreeNode newNodeParent=(DefaultMutableTreeNode)newNode.getParent(); if(newNodeParent!=null && !newNodeParent.toString().equals(path.getText())){ return getNodePath(newNodeParent)+"//"+newNode.toString(); } else{ return newNode.toString(); } } }

FileContent类:

package org.lee.file; import java.io.*; import java.util.*; public class FileContent { private HashMap<String,String> fileMess=new HashMap<String,String>(); private File file; //文件夹的相关属性 private String fileType; private String fileName; private String fileLocation; private Long fileSize=0L; private int fileFolder=-1; private int fileCount=0; public FileContent(File file){ this.file=file; init(); } public void init(){ if(file.isDirectory()){ fileType="文件夹"; parserFile(file); } else{ fileType=getSuffix(file)+"文件"; fileSize=file.length(); fileFolder=0; fileCount=0; } fileLocation=file.getParent(); fileName=file.getName(); fileMess.put("文件名:", fileName); fileMess.put("文件类型:", fileType); fileMess.put("文件位置:", fileLocation); fileMess.put("文件大小:", String.valueOf(fileSize)+"KB"); fileMess.put("包含:", String.valueOf(fileFolder)+"个文件夹,"+ String.valueOf(fileCount)+"个文件"); } public void parserFile(File f){ if(f.isDirectory()){ fileFolder++; File[] files=f.listFiles(); for(File fi : files){ parserFile(fi); } } else{ fileSize+=f.length(); fileCount++; } } @Override public String toString(){ StringBuilder str=new StringBuilder(); for(String key : fileMess.keySet()){ str.append(key+fileMess.get(key)+"/r/n"); } return str.toString(); } /* * 获取后缀名 */ public String getSuffix(File f){ String name=f.getName(); return name.substring(name.lastIndexOf(".")+1); } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileLocation() { return fileLocation; } public void setFileLocation(String fileLocation) { this.fileLocation = fileLocation; } public Long getFileSize() { return fileSize; } public void setFileSize(Long fileSize) { this.fileSize = fileSize; } public int getFileFolder() { return fileFolder; } public void setFileFolder(int fileFolder) { this.fileFolder = fileFolder; } public int getFileCount() { return fileCount; } public void setFileCount(int fileCount) { this.fileCount = fileCount; } }

你可能感兴趣的:(查看文件内容)