1. /*
2. *Main-class : FileOperator
3. *Author : HXJie
4. *Version : 1.6(j2sdk)
5. */
6.
7.
8. import java.awt.*;
9. import javax.swing.*;
10. import java.awt.event.*;
11. import java.io.*;
12. import java.util.*;
13. import javax.swing.event.*;
14.
15. public class
16. private JFrame frame; //主窗口
17. private JTextField fileText, sizeText; //分别为显示文件路径和文件大小窗口
18. private JList fileList; //分割后文件列表以及合并关文件列表
19. private JButton splitB, clearB, flopyB, fileB, listB; //分割、清空、合并、打开被分割文件、打开被合并文件
20. private JLabel sizeL; //显示KB
21. private Actions action; //处理文件操作
22. private JFileChooser fileChooser; //文件选择
23. private File file ; // 被分割的文件
24. private String title; // 窗口标题
25. private int maxLen ; //文件分割大小
26. private Vector listV; // 构造文件列表
27. private Font font; //设置按钮字体
28.
29. private
30. new
31. new JFileChooser(".");
32. new MyFileFilter("java"));
33. new MyFileFilter("txt"));
34. true);
35. null;
36. this.title = s;
37. maxLen = 0;
38. new
39. new Font("Dialog", Font.PLAIN, 14);
40. }
41. public
42.
43. private void
44.
45. new JFrame("文件分割与合并");
46. Container pane = frame.getContentPane();
47. new
48.
49. new JPanel(new
50. headP.setBorder(BorderFactory.createLineBorder(Color.white,2));
51. new JButton("选择要被分割的文件");
52. fileB.setFont(font);
53. fileB.setForeground(Color.black);
54. fileB.setBorder(BorderFactory.createBevelBorder(1,Color.black,Color.pink));
55. this);
56. new JTextField("没有选择任何文件…………",25);
57. fileText.setBackground(Color.black);
58. fileText.setForeground(Color.blue);
59. false);
60. fileText.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
61. headP.add(fileB);
62. headP.add(fileText);
63. pane.add(headP,BorderLayout.NORTH);
64.
65. new JPanel(new
66. centerP.setBorder(BorderFactory.createBevelBorder(1,Color.black,Color.pink));
67. new JButton("选择要合并的文件(可以按Shift或Ctrl进行)");
68. listB.setFont(font);
69. listB.setForeground(Color.black);
70. listB.setBorder(BorderFactory.createBevelBorder(1, Color.black, Color.pink));
71. this);
72. new
73. fileList.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
74. fileList.setBackground(Color.black);
75. fileList.setForeground(Color.blue);
76. new
77. public void
78. if(me.getClickCount() == 2)
79. mouseOperate(me);
80. }
81. });
82. centerP.add(listB);
83. new
84. pane.add(centerP,BorderLayout.CENTER);
85.
86. new JPanel(new
87. southP.setBorder(BorderFactory.createLoweredBevelBorder());
88. new
89. sizeText.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
90. new JLabel("KB (1024KB=1M)");
91. new JButton("分割");
92. splitB.setFont(font);
93. splitB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
94. this);
95. new JButton("清空");
96. clearB.setFont(font);
97. clearB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
98. this);
99. new JButton("合并");
100. flopyB.setFont(font);
101. flopyB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
102. this);
103. southP.add(sizeText);
104. southP.add(sizeL);
105. southP.add(splitB);
106. southP.add(clearB);
107. southP.add(flopyB);
108. pane.add(southP,BorderLayout.SOUTH);
109.
110. Toolkit tk = Toolkit.getDefaultToolkit();
111. Dimension d = tk.getScreenSize();
112. frame.setLocation(d.height/2, d.width/6);
113. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114. frame.setBackground(Color.black);
115. frame.setForeground(Color.black);
116. false);
117. frame.setSize(400,320);
118. frame.setTitle(title);
119. true);
120. }
121.
122. public static void
123. new FileOperator("").init();
124. }
125.
126. public void
127. if(e.getSource() == fileB) {
128. //fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
129. int
130. if(result == JFileChooser.APPROVE_OPTION) {
131. file = fileChooser.getSelectedFile();
132. if(file != null) {
133. fileText.setText(file.getAbsolutePath().toString());
134. false);
135. false);
136. }
137. }
138. }
139. if(e.getSource() == listB) {
140. null;
141. int
142. if(result == JFileChooser.APPROVE_OPTION) {
143. files = fileChooser.getSelectedFiles();
144. file = files[0];
145. if(files != null) {
146. for(int
147. listV.addElement(files[i].getAbsolutePath().toString());
148. fileList.setListData(listV);
149. false);
150. false);
151. }
152. }
153. }
154. if(e.getSource() == clearB) {
155. null;
156. "没有选择任何文件…………");
157. listV.clear();
158. fileList.setListData(listV);
159. action.clear();
160. true);
161. true);
162. true);
163. true);
164. }
165. if(e.getSource() == splitB) {
166.
167. if(fileText.getText().equals("没有选择任何文件…………")) {
168. "先选择要分割的文件……");
169. return;
170. }
171. else if(sizeText.getText().equals("")) {
172. "请指定单个文件的大小……");
173. return;
174. }
175. else
176. //检测输入的合法性
177. if(maxLen == 0) return;
178. new Thread(new Runnable() { //在新线程中启动文件操作可以消除画面不能重绘
179. public void
180. false);
181. false);
182. false);
183. false);
184. if(action.split(file,maxLen) ) { //Actions类中的 split()方法返回一个boolean值
185. listV.addAll(action.getListData());
186. fileList.setListData(listV);
187. "分割完成……");
188. true);
189. }
190. else
191. "分割失败……");
192. Vector tempV = action.getListData();
193. for(int
194. new
195. tempF.delete();
196. }
197. true);
198. }
199. }
200. });
201. splitT.start();
202. }
203. }
204. if(e.getSource() == flopyB) {
205. if(listV.isEmpty()) {
206. "请选择要合并的文件……");
207. return;
208. }
209. "输入新文件的名字(文件放在第一个文件的文件夹下):","Name",
210. JOptionPane.QUESTION_MESSAGE);
211. if(name == null) return;
212. "//" + name +"." + action.getConName(file);
213. new
214. if(desFile.exists()) {
215. "The file has exist--change another one...");
216. return;
217. }
218. fileText.setText(path);
219. new
220. for(int
221. get(i);
222. new Thread(new
223. public void
224. false);
225. false);
226. false);
227. false);
228. if(action.floppy(listElements,(String)fileText.getText())) {
229. "合并完成……");
230. }
231. else
232. "合并失败……");
233. desFile.delete();
234. }
235. true);
236. }
237. });
238. floppyT.start();
239. }
240. }
241. private void
242. "Warning...",JOptionPane.WARNING_MESSAGE);
243. }
244. private int
245. char[] strChr = strValue.toCharArray();
246. for(int
247. if(!Character.isDigit(strChr[i])) {
248. "The size is inlleagle...");
249. return
250. }
251. }
252. return
253. }
254. public String getConName(File file) { //获取文件后辍
255. String fName = file.getName();
256. int index = fName.lastIndexOf(".");
257. return
258. }
259.
260. public void mouseOperate(MouseEvent me) { //处理双击
261. int
262. listV.removeElementAt(index);
263. fileList.setListData(listV);
264. }
265. }
266.
267.
268.
269. import java.awt.*;
270. import java.awt.event.*;
271. import javax.swing.*;
272. import java.util.*;
273. import java.io.*;
274.
275. public class
276.
277. private Vector listData; //存放文件列表
278. private int fileCount ; //在分割时记录文件数
279. private int lenCount; //在分割文件时判定文件是否大于最大值
280. private File currFile;//当前文件
281. private BufferedInputStream in;
282. private BufferedOutputStream out;
283.
284. public
285. new
286. fileCount = 0;
287. lenCount =0;
288. }
289. public void
290. fileCount = 0;
291. lenCount = 0;
292. null;
293. listData.clear();
294. }
295. public boolean split(File file, int
296. currFile = file;
297. null; //外观
298. try
299. in = new BufferedInputStream(new
300. new ProgressMonitorInputStream(null,"请等待...",in);
301. out = new
302. ProgressMonitor pm = pmin.getProgressMonitor();
303. int get
304. while((get
305. lenCount++;
306. if(pm.isCanceled()) {
307. return false;
308. }
309. if(lenCount > length*1024) { //如果已经读取最大字节数则再获取下一个输出流
310. out.close();
311. out = new
312. lenCount = 0;
313. }
314. out.write((char)(get));
315. }
316. return true;
317. }
318. catch(IOException ioe) {}
319. finally
320. try
321. if(in != null) in.close();
322. if(out != null) out.close();
323. if(pmin != null) pmin.close();
324. }
325. catch(IOException ioe2) {}
326. }
327. return false;
328. }
329. public
330. new ProgressMonitor(null, "Reading...","",0,100);
331. pm.setMillisToDecideToPopup(0);
332. pm.setMillisToPopup(1000);
333. int
334. int
335. int
336. for(int
337. int) (new
338. }
339. //上面是构造界面
340.
341. null;
342. try
343. out = new BufferedOutputStream(new
344. for(int i = 0; i < elements.length; i++) { //一个文件读完获取下一个文件
345. if(i > 0) {
346. in.close ();
347. }
348. in = new
349. "Reading...."
350. int
351. while((chr = in.read()) != -1) {
352. if(pm.isCanceled())
353. return false;
354. out.write(chr);
355. count++;
356. if(count % 1024 == 0) { //每1M 前进一格
357. index++;
358. pm.setProgress(index);
359. }
360. }
361. }
362. return true;
363. }
364. catch(IOException ioe) {}
365. finally
366. try
367. if(in != null) in.close();
368. if(out != null) out.close();
369. }
370. catch(IOException ioe2) {}
371. }
372. return false;
373. }
374. public
375. return
376. }
377. public void
378. listData.addElement(name);
379. }
380. public
381. fileCount++;
382. null;
383. "//" + getSuffName(currFile) + "_" + fileCount + "." + getConName(currFile);
384. setListData(filename);
385. try
386. new
387. }
388. catch(IOException ioe) {}
389. return
390. }
391. public
392. try
393. return new
394. }
395. catch(IOException ioe) {}
396. return null;
397. }
398. public String getConName(File file) { // 获取后辍
399. String fName = file.getName();
400. int index = fName.lastIndexOf(".");
401. return
402. }
403. public String getSuffName(File file) {//获取文件名字
404. String fName = file.getName();
405. int index = fName.lastIndexOf(".");
406. return
407. }
408. }
409.
410.
411.
412. import java.io.File;
413. import javax.swing.filechooser.*;
414.
415. public class
416.
417. String name;
418.
419. public
420. this.name = s;
421. }
422.
423. public
424. if(file.isDirectory()) return true;
425. String filename = file.getName();
426. int index = filename.lastIndexOf(".");
427. if(index > 0 && index < filename.length()-1) {
428. String temp = filename.substring(index+1).toLowerCase();
429. if(temp.equals(name)) return true;
430. }
431. return false;
432. }
433. public
434. if(name.equals("txt"))
435. return "Text File(*.txt)";
436. if(name.equals("java"))
437. return "Java File(*.java)";
438. return "";
439. }
440. }
/*
*Main-class : FileOperator
*Author : HXJie
*Version : 1.6(j2sdk)
*/import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;public class FileOperator implements ActionListener {
private JFrame frame; //主窗口
private JTextField fileText, sizeText; //分别为显示文件路径和文件大小窗口
private JList fileList; //分割后文件列表以及合并关文件列表
private JButton splitB, clearB, flopyB, fileB, listB; //分割、清空、合并、打开被分割文件、打开被合并文件
private JLabel sizeL; //显示KB
private Actions action; //处理文件操作
private JFileChooser fileChooser; //文件选择
private File file ; // 被分割的文件
private String title; // 窗口标题
private int maxLen ; //文件分割大小
private Vector listV; // 构造文件列表
private Font font; //设置按钮字体 private FileOperator(String s) {
action = new Actions();
fileChooser = new JFileChooser(".");
fileChooser.setFileFilter(new MyFileFilter("java"));
fileChooser.setFileFilter(new MyFileFilter("txt"));
fileChooser.setMultiSelectionEnabled(true);
file = null;
this.title = s;
maxLen = 0;
listV = new Vector();
font = new Font("Dialog", Font.PLAIN, 14);
}
public FileOperator() { } private void init() {
frame = new JFrame("文件分割与合并");
Container pane = frame.getContentPane();
pane.setLayout(new BorderLayout()); JPanel headP = new JPanel(new GridLayout(2,1));
headP.setBorder(BorderFactory.createLineBorder(Color.white,2));
fileB = new JButton("选择要被分割的文件");
fileB.setFont(font);
fileB.setForeground(Color.black);
fileB.setBorder(BorderFactory.createBevelBorder(1,Color.black,Color.pink));
fileB.addActionListener(this);
fileText = new JTextField("没有选择任何文件…………",25);
fileText.setBackground(Color.black);
fileText.setForeground(Color.blue);
fileText.setEditable(false);
fileText.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
headP.add(fileB);
headP.add(fileText);
pane.add(headP,BorderLayout.NORTH); JPanel centerP = new JPanel(new FlowLayout(FlowLayout.CENTER));
centerP.setBorder(BorderFactory.createBevelBorder(1,Color.black,Color.pink));
listB = new JButton("选择要合并的文件(可以按Shift或Ctrl进行)");
listB.setFont(font);
listB.setForeground(Color.black);
listB.setBorder(BorderFactory.createBevelBorder(1, Color.black, Color.pink));
listB.addActionListener(this);
fileList = new JList();
fileList.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
fileList.setBackground(Color.black);
fileList.setForeground(Color.blue);
fileList.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if(me.getClickCount() == 2)
mouseOperate(me);
}
});
centerP.add(listB);
centerP.add(new JScrollPane(fileList));
pane.add(centerP,BorderLayout.CENTER); JPanel southP = new JPanel(new FlowLayout(FlowLayout.CENTER));
southP.setBorder(BorderFactory.createLoweredBevelBorder());
sizeText = new JTextField(6);
sizeText.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
sizeL = new JLabel("KB (1024KB=1M)");
splitB = new JButton("分割");
splitB.setFont(font);
splitB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
splitB.addActionListener(this);
clearB = new JButton("清空");
clearB.setFont(font);
clearB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
clearB.addActionListener(this);
flopyB = new JButton("合并");
flopyB.setFont(font);
flopyB.setBorder(BorderFactory.createEtchedBorder(Color.black,Color.pink));
flopyB.addActionListener(this);
southP.add(sizeText);
southP.add(sizeL);
southP.add(splitB);
southP.add(clearB);
southP.add(flopyB);
pane.add(southP,BorderLayout.SOUTH); Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
frame.setLocation(d.height/2, d.width/6);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.black);
frame.setForeground(Color.black);
frame.setResizable(false);
frame.setSize(400,320);
frame.setTitle(title);
frame.setVisible(true);
} public static void main(String[] args) {
new FileOperator("").init();
} public void actionPerformed(ActionEvent e) {
if(e.getSource() == fileB) {
//fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
if(file != null) {
fileText.setText(file.getAbsolutePath().toString());
listB.setEnabled(false);
flopyB.setEnabled(false);
}
}
}
if(e.getSource() == listB) {
File[] files = null;
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION) {
files = fileChooser.getSelectedFiles();
file = files[0];
if(files != null) {
for(int i = 0; i < files.length; i++)
listV.addElement(files[i].getAbsolutePath().toString());
fileList.setListData(listV);
fileB.setEnabled(false);
splitB.setEnabled(false);
}
}
}
if(e.getSource() == clearB) {
file = null;
fileText.setText("没有选择任何文件…………");
listV.clear();
fileList.setListData(listV);
action.clear();
splitB.setEnabled(true);
flopyB.setEnabled(true);
fileB.setEnabled(true);
listB.setEnabled(true);
}
if(e.getSource() == splitB) { if(fileText.getText().equals("没有选择任何文件…………")) {
showMessage("先选择要分割的文件……");
return;
}
else if(sizeText.getText().equals("")) {
showMessage("请指定单个文件的大小……");
return;
}
else {
maxLen = checkValue((String)sizeText.getText().trim()); //检测输入的合法性
if(maxLen == 0) return;
Thread splitT = new Thread(new Runnable() { //在新线程中启动文件操作可以消除画面不能重绘
public void run() {
splitB.setEnabled(false);
flopyB.setEnabled(false);
clearB.setEnabled(false);
fileB.setEnabled(false);
if(action.split(file,maxLen) ) { //Actions类中的 split()方法返回一个boolean值
listV.addAll(action.getListData());
fileList.setListData(listV);
showMessage("分割完成……");
clearB.setEnabled(true);
}
else {
showMessage("分割失败……");
Vector tempV = action.getListData();
for(int i = 0; i < tempV.size(); i++) {
File tempF = new File((String)tempV.elementAt(i));
tempF.delete();
}
clearB.setEnabled(true);
}
}
});
splitT.start();
}
}
if(e.getSource() == flopyB) {
if(listV.isEmpty()) {
showMessage("请选择要合并的文件……");
return;
}
String name = (String)JOptionPane.showInputDialog(frame,"输入新文件的名字(文件放在第一个文件的文件夹下):","Name",
JOptionPane.QUESTION_MESSAGE);
if(name == null) return;
String path = file.getParent()+"//" + name +"." + action.getConName(file);
final File desFile = new File(path);
if(desFile.exists()) {
showMessage("The file has exist--change another one...");
return;
}
fileText.setText(path);
final String[] listElements = new String[listV.size()];
for(int i = 0; i < listV.size(); i++)
listElements[i] = (String)listV.get(i);
Thread floppyT = new Thread(new Runnable() {
public void run() {
flopyB.setEnabled(false);
splitB.setEnabled(false);
clearB.setEnabled(false);
listB.setEnabled(false);
if(action.floppy(listElements,(String)fileText.getText())) {
showMessage("合并完成……");
}
else {
showMessage("合并失败……");
desFile.delete();
}
clearB.setEnabled(true);
}
});
floppyT.start();
}
}
private void showMessage(String message) {
JOptionPane.showMessageDialog(frame,message,"Warning...",JOptionPane.WARNING_MESSAGE);
}
private int checkValue(String strValue) {
char[] strChr = strValue.toCharArray();
for(int i = 0; i < strChr.length; i++) {
if(!Character.isDigit(strChr[i])) {
showMessage("The size is inlleagle...");
return 0;
}
}
return Integer.valueOf(strValue).intValue();
}
public String getConName(File file) { //获取文件后辍
String fName = file.getName();
int index = fName.lastIndexOf(".");
return fName.substring(index+1);
} public void mouseOperate(MouseEvent me) { //处理双击
int index = fileList.locationToIndex(me.getPoint());
listV.removeElementAt(index);
fileList.setListData(listV);
}
}
java并发合并文件 java合并文件内容
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java 合并word文件 java wav文件合并
以任意字节切割mp3歌之后再拼接成完整的案例1:方法1方法2案例2将多首歌拼接成一首大合唱,优化代码 对一首歌或者多首歌按照1Mb进行切割保存,然后再将切割后的碎片拼接成一首的完整的歌,可以拼接多首歌.来个几十兆的大合唱.案例1:对一首歌按照1Mb进行切割保存,然后再将切割后的碎片拼接成一首的完整的歌 将g:\夜夜夜夜.mp3 切开存储g:\apartFile文件夹中 再访问文件夹拼接完整.方法
java 合并word文件 切割拼接音乐 IO流 RandomAccessFile SequenceInputStream