原因:
因為在網上下載視頻教程,有的名字特別長,一般都是機構或者網站的宣傳,不方便直接看到視頻的簡介,所以做了下面的第一個功能。
因為老師發的課件中,文件夾太多,想把docx都放在同一個文件夾下面,一個一個找出來太麻煩,所以做了第二個功能。
最近剛剛學了Java文件和流的知識,所以正好練練手,這也是自己的第一個exe程序,分享一下哈。
(導出jar文件,以及用工具exe4j生成exe文件,這部分省略了哈)
用到的知識:
用到Java中文件,流的知識,以及簡單的GUI知識。
功能:
功能一:去除文件名字的某些關鍵字,也可以設置代替字。
功能二:提取一個路徑下面所有特定類型的文件,然后放在一個新的文件夾下面,如果有重復的文件,則自動排序在后面加數字來區分。
先看下啟動后的界面和生成的exe文件:
第一個功能演示:
沒有操作前的:
操作后:把前面部分相同關鍵字全部去掉了
還有撤回功能:
第二個功能演示:
沒有操作前:
操作后:
當然,也有撤回功能
源代碼分析:
啟動類:
1
2
3
4
5
6
7
|
package guuze; public class Test { public static void main(String[] args) { //啟動GUI,即用戶界面 new ShowGui(); } } |
顯示GUI類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
package guuze; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class ShowGui { private JFrame f; private JButton b1; private JButton b2; private JButton b3; private JTextField tf1; private JTextField tf2; private JTextField tf3; private JButton b4; private JButton b5; private JButton b6; private JTextField tf4; private JTextField tf5; private JTextField tf6; private static String s1; public ShowGui() { // 直接調用 startGui(); } public void startGui() { f.setLayout( new FlowLayout()); Image icon = Toolkit.getDefaultToolkit().getImage( "image/4.jpg" ); // 設置左上角logo圖標 f.setIconImage(icon); // 6個按鈕 b1 = new JButton( "開始" ); b2 = new JButton( "撤回" ); b3 = new JButton( "退出" ); b4 = new JButton( "一鍵提取" ); b5 = new JButton( "撤回" ); b6 = new JButton( "退出" ); // 6個按鈕的大小 b1.setPreferredSize( new Dimension( 89 , 39 )); b2.setPreferredSize( new Dimension( 89 , 39 )); b3.setPreferredSize( new Dimension( 89 , 39 )); b4.setPreferredSize( new Dimension( 89 , 39 )); b5.setPreferredSize( new Dimension( 89 , 39 )); b6.setPreferredSize( new Dimension( 89 , 39 )); // 6個 文本框的大小以及輸入字體的屬性 tf1 = new JTextField( "Please input absolute_path" , 40 ); tf1.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf1.setBounds( 200 , 15 , 550 , 126 ); tf2 = new JTextField( "Please input keyWords" , 40 ); tf2.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf2.setBounds( 200 , 15 , 550 , 126 ); tf3 = new JTextField( "Please input replaceWords" , 40 ); tf3.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf3.setBounds( 200 , 15 , 550 , 126 ); tf4 = new JTextField( "Please input absolute_path" , 40 ); tf4.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf4.setBounds( 200 , 15 , 550 , 126 ); tf5 = new JTextField( "Please input target_path" , 40 ); tf5.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf5.setBounds( 200 , 15 , 550 , 126 ); tf6 = new JTextField( "Please input filetype" , 40 ); tf6.setFont( new Font( "宋體" , Font.PLAIN, 25 )); tf6.setBounds( 200 , 15 , 550 , 126 ); // 把按鈕和文本框添加上 f.add(tf1); f.add(tf2); f.add(tf3); f.add(b1); f.add(b2); f.add(b3); f.add(tf4); f.add(tf5); f.add(tf6); f.add(b4); f.add(b5); f.add(b6); // 調用事件監聽函數 myEvent(); f.setVisible( true ); } private void myEvent() { // 點擊右上角×退出 f.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit( 0 ); } }); // 點擊第一個按鈕的響應事件 b1.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { s1 = tf1.getText(); // 對文本框內值進行判斷,如果什么也沒寫,當做空處理,以下的類似 if (s1.equals( "Please input path" )) { s1 = "" ; } File file = new File(s1); String test[]; test = file.list(); RenameFunction.test1 = test; String s2 = tf2.getText(); if (s2.equals( "Please input replaceWords" )) { s2 = "" ; } String s3 = tf3.getText(); if (s3.equals( "Please input replaceWords" )) { s3 = "" ; } try { // 啟動重命名函數 RenameFunction.sure(s1, s2, s3); } catch (Exception e1) { } } }); // 點擊第二個按鈕的響應事件 b2.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { try { try { if (!s1.equals( "Please input path" )) { // 啟動撤回 RevokeRename.revoke(s1); } } catch (Exception e2) { } } catch (Exception e1) { } } }); // 點擊第三個按鈕的響應事件 b3.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.exit( 0 ); // 退出 } }); // 點擊第四個按鈕的響應事件 b4.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { String s2 = tf4.getText(); String s3 = tf5.getText(); String s4 = tf6.getText(); if (s2.equals( "Please input absolute_path" )) { s2 = "" ; } if (s3.equals( "Please input target_path" )) { s3 = "" ; } if (s4.equals( "Please input filetype" )) { s4 = "" ; } // 啟動文件搜索函數 SearchFileFunction.startCopy(s2, s3, s4); } }); // 點擊第五個按鈕的響應事件 b5.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { String s2 = tf5.getText(); // 啟動撤回函數 RemoveTargetFile.startDelete(s2); } }); // 點擊第六個按鈕的響應事件 b6.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.exit( 0 ); // 退出 } }); } } |
GUI背景圖片設置類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package guuze; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class BgSet extends JFrame { private static final long serialVersionUID = 1L; public BgSet() { // 設置標題 super ( "GreatFish" ); setBounds( 100 , 100 , 600 , 600 ); // 背景圖片的路徑。 String path = "image/3.jpg" ; ImageIcon background = new ImageIcon(path); JLabel label = new JLabel(background); label.setBounds( 0 , 0 , this .getWidth(), this .getHeight()); JPanel imagePanel = (JPanel) this .getContentPane(); imagePanel.setOpaque( false ); this .getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); } } |
文件重命名類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package guuze; import java.io.File; import java.util.Scanner; public class RenameFunction { static Scanner input = new Scanner(System.in); public static String test1[]; public static void sure(String s1, String s2, String s3) throws Exception { File file = new File(s1); String test[]; test = file.list(); // 遍歷文件的名字 for ( int i = 0 ; i < test.length; i++) { // 判斷是不是有你想去除的關鍵字 if (test[i].indexOf(s2) != - 1 ) { // 保存重命名后的文件名 test[i] = test[i].replace(s2, s3); } } File[] files = file.listFiles(); for ( int i = 0 ; i < test.length;) { for (File f : files) { if (f.isFile()) { // 循環賦重命名后的名字 f.renameTo( new File(s1 + "/" + test[i++])); } } } } } |
文件重命名撤回函數類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package guuze; import java.io.File; public class RevokeRename { public static void revoke(String s1) throws Exception { // 重新賦回原來的名字 File file = new File(s1); File[] files = file.listFiles(); for ( int i = 0 ; i < RenameFunction.test1.length;) { for (File f : files) { if (f.isFile()) { // 注意是test1 f.renameTo( new File(s1 + "/" + RenameFunction.test1[i++])); } } } } } |
文件復制類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package guuze; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class SearchFileFunction { static int count = 1 ; public static void startCopy(String source_path, String target_path, String file_Type) { // 啟動循環函數 xunHuan(source_path, target_path, file_Type); } public static void xunHuan(String source_path, String target_path, String file_Type) { File file = new File(source_path); String names[] = file.list(); // 判斷是不是文件以及是否以你想要的文件類型結尾 if (file.isFile() && file.getAbsolutePath().endsWith(file_Type)) { String new_path = target_path + "/" + file.getName(); File file1 = new File(new_path); if (!file1.exists()) { try { file1.createNewFile(); } catch (IOException e) { } } else { // 如果文件名字相同,在點前面加數字進行區分 // 注意用\\.進行分隔,而不是. String[] arr = new_path.split( "\\." ); String new_path1 = arr[ 0 ] + count + "." + arr[ 1 ]; file1.renameTo( new File(new_path1)); } // 是文件,所以開始復制文件 fileCopyByBufferStreamArray(file.getAbsolutePath(), new_path); } else if (file.isFile() && !file.getAbsolutePath().endsWith(file_Type)) { // 注意這個方法體中什么都不寫,就是不做處理 } else { for ( int i = 0 ; i < names.length; i++) { // 不是文件,進行迭代 xunHuan(file.getAbsolutePath() + "/" + names[i], target_path, file_Type); } } } public static void fileCopyByBufferStreamArray(String srcFile, String targetFile) { // 用流的知識進行寫文件 File file = new File(srcFile); File file1 = new File(targetFile); FileInputStream fis = null ; FileOutputStream fos = null ; BufferedInputStream bis = null ; BufferedOutputStream bos = null ; try { fis = new FileInputStream(file); fos = new FileOutputStream(file1); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); int len = 0 ; byte [] b = new byte [ 10 ]; while ((len = bis.read(b)) != - 1 ) { bos.write(b, 0 , len); } bos.flush(); } catch (IOException e) { } finally { try { fis.close(); fos.close(); bis.close(); bos.close(); } catch (IOException e) { } } } } |
文件復制撤回類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package guuze; import java.io.File; public class RemoveTargetFile { public static void startDelete(String path) { File file = new File(path); deleteFile(file); } private static void deleteFile(File file) { // 記住不要把路徑的那個文件夾刪掉了 if (file.exists()) { if (file.isFile()) { // 是文件,直接刪除 file.delete(); } else if (file.isDirectory()) { File[] files = file.listFiles(); for ( int i = 0 ; i < files.length; i++) { // 如果不是文件,進行迭代 deleteFile(files[i]); } } } } } |
以上所述是小編給大家介紹的Java文件批量重命名批量提取特定類型文件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!