一个文件浏览的程序

最近写了一个文件查看程序,挺简陋的,但里面的树形浏览JTree使用还是有些许作用:

文件内容类:

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)+"字节"); 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(); if(name.lastIndexOf(".")>0) return name.substring(name.lastIndexOf(".")+1); else return "未知"; } 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; } }

主窗体类:

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(); } } }

主类

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); } }); } }  

你可能感兴趣的:(一个文件浏览的程序)