1 import java.awt.BorderLayout;
2 import java.awt.Color;
3 import java.awt.Font;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.File;
7 import java.util.HashMap;
8 import javax.swing.JComboBox;
9 import javax.swing.JFrame;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.JScrollPane;
13 import javax.swing.JTextArea;
14 import javax.swing.border.EmptyBorder;
15
16 /
17 * 文件排序系统。
18 *
19 * @author
20
21 /
22 public class FileUtils {
23
24 /
25 * 用于存储分类后的文件。
26 * key:后缀名, value:StringBuilder存储对应的文件。
27 */
28 private HashMap<String, StringBuilder> resultMap = new HashMap<String, StringBuilder>();
29
30 /**
31 * 监听文件目录。
32 *
33 * @param dir 目录。
34 * @throws IllegalAccessException 访问非法异常。
35 */
36 public void listenDirectory(File dir) throws IllegalAccessException {
37 if (!dir.exists()) {
38 throw new IllegalAccessException("目录" + dir + "不存在。");
39 }
40
41 if (!dir.isDirectory()) {
42 throw new IllegalArgumentException(dir + "不是目录");
43 }
44
45 String[] fileNames = dir.list();
46 resultMap.put("all", new StringBuilder()); //默认所有文件。
47 resultMap.put("folder", new StringBuilder()); //文件夹形式。
48
49 //后缀。
50 String suffix;
51 for (String fileName : fileNames) {
52 resultMap.get("all").append(fileName + "\n");
53 if (fileName.indexOf(".") > 0) {
54 suffix = fileName.substring(fileName.indexOf("."), fileName.length());
55
56 if (!resultMap.containsKey(suffix)) {
57 StringBuilder stringBuilder = new StringBuilder();
58 stringBuilder.append(fileName + "\n");
59 resultMap.put(suffix, stringBuilder);
60 } else {
61 resultMap.get(suffix).append(fileName + "\n");
62 }
63 } else {
64 resultMap.get("folder").append(fileName + "\n");
65 }
66 }
67
68 buildGUI();
69 }
70
71 /**
72 * 搭建GUI。
73 */
74 public void buildGUI() {
75 final JTextArea fileList = new JTextArea();
76 fileList.setText(resultMap.get("all").toString());
77 String[] likes = new String[resultMap.keySet().size()];
78 resultMap.keySet().toArray(likes);
79 final JComboBox combox = new JComboBox(likes);
80
81 JFrame frm = new JFrame();
82 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
83
84 JPanel contentPane = new JPanel();
85 contentPane.setBorder(new EmptyBorder(6, 10, 10, 10));
86 contentPane.setLayout(new BorderLayout(5, 5));
87 contentPane.add(combox, BorderLayout.NORTH);
88
89 JPanel pane = new JPanel();
90 pane.setLayout(new BorderLayout(8, 8));
91
92 JLabel label = new JLabel(" File lists :");
93 label.setFont(new Font("Serif", Font.PLAIN, 16));
94
95 fileList.setForeground(new Color(140, 171, 226));
96 fileList.setBackground(Color.white);
97 fileList.setSelectedTextColor(new Color(87, 49, 134));
98 fileList.setForeground(Color.black);
99
100 JScrollPane scrollPane = new JScrollPane(fileList);
101 scrollPane.setColumnHeaderView(label);
102
103 pane.add(scrollPane, BorderLayout.CENTER);
104 contentPane.add(pane, BorderLayout.CENTER);
105 frm.add(contentPane);
106 frm.setBounds(500, 300, 300, 400);
107 frm.setVisible(true);
108
109 //JComboBox事件监听。
110 combox.addActionListener(new ActionListener() {
111 @Override
112 public void actionPerformed(ActionEvent e) {
113 try {
114 // 获取组合框的item
115 String item = (String) combox.getItemAt(combox.getSelectedIndex());
116 fileList.setText(resultMap.get(item).toString());
117 } catch (Exception e1) {
118 e1.printStackTrace();
119 }
120 }
121 });
122 }
123
124 public static void main(String[] args) {
125 String path = "D:\";
126 try {
127 new FileUtils().listenDirectory(new File(path));
128 } catch (IllegalAccessException e) {
129 e.printStackTrace();
130 }
131 }
132 }
转载于:https://www.cnblogs.com/liangjingjing123/p/5401151.html