📄 filesplit.java
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
public class FileSplit extends JFrame implements ActionListener {
private JButton jbtOpen = new JButton("打开");
private JButton jbtOpen2 = new JButton("选取");
private JButton jbtSplit = new JButton("拆分");
private JButton jbtCompages = new JButton("组合");
private JLabel labelOpen = new JLabel("源文件为");
private JLabel labelOpen2 = new JLabel("选取文件为");
private JLabel labelSplit = new JLabel("分割份数:");
private JTextField textSplit = new JTextField(5);
private JProgressBar progressBar = new JProgressBar();
private JProgressBar progressBar2 = new JProgressBar();
private JFileChooser fileChooser = new JFileChooser();
private int acount = 0;
private File file = null;
private File[] files = null;
public FileSplit() {
this.setSize(350,300);
this.setTitle("文件分割机(制作人:白洋)");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
Box box1 = Box.createHorizontalBox();
box1.add(Box.createHorizontalStrut(5));
box1.add(jbtOpen);
box1.add(Box.createHorizontalStrut(10));
box1.add(labelOpen);
box1.add(Box.createHorizontalGlue());
Box box2 = Box.createHorizontalBox();
box2.add(Box.createHorizontalStrut(5));
box2.add(labelSplit);
box2.add(Box.createHorizontalStrut(5));
textSplit.setMaximumSize(textSplit.getPreferredSize());
box2.add(textSplit);
box2.add(Box.createHorizontalGlue());
Box box3 = Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(5));
box3.add(jbtSplit);
box3.add(Box.createHorizontalStrut(10));
box3.add(progressBar);
box3.add(Box.createHorizontalGlue());
Box box4 = Box.createHorizontalBox();
box4.add(Box.createHorizontalStrut(5));
box4.add(jbtOpen2);
box4.add(Box.createHorizontalStrut(10));
box4.add(labelOpen2);
box4.add(Box.createHorizontalGlue());
Box box5 = Box.createHorizontalBox();
box5.add(Box.createHorizontalStrut(5));
box5.add(jbtCompages);
box5.add(Box.createHorizontalStrut(10));
box5.add(progressBar2);
box5.add(Box.createHorizontalGlue());
Box vBox1 = Box.createVerticalBox();
vBox1.add(box1);
vBox1.add(box2);
vBox1.add(box3);
vBox1.setBorder(BorderFactory.createTitledBorder("分割文件"));
Box vBox2 = Box.createVerticalBox();
vBox2.add(box4);
vBox2.add(box5);
vBox2.setBorder(BorderFactory.createTitledBorder("组合文件"));
Box vBox = Box.createVerticalBox();
vBox.add(Box.createVerticalGlue());
vBox.add(vBox1);
vBox.add(Box.createVerticalStrut(5));
vBox.add(vBox2);
vBox.add(Box.createVerticalGlue());
contentPane.add(vBox);
jbtOpen.addActionListener(this);
jbtOpen2.addActionListener(this);
jbtSplit.addActionListener(this);
jbtCompages.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
//选择源文件
if (event.getSource().equals(jbtOpen)) {
int chooseOK = 0;
fileChooser.setCurrentDirectory(new File("."));//设置当前工作目录
chooseOK = fileChooser.showOpenDialog(this);
if (chooseOK == 0) {
file = fileChooser.getSelectedFile();
}
else {
return;
}
//size计算出源文件的大小
double size = file.length() / 1024.0;
size = Math.round(size * 100);
size = size / 100;
labelOpen.setText("源文件大小为:" + size + "KB");
//acount计算出for循环的总次数
byte[] b = new byte[512];
acount = (int)(file.length()/b.length);
if (file.length()%b.length != 0) {
acount++;
}
progressBar.setMaximum(acount);
}
//选择组合文件文件夹
if (event.getSource().equals(jbtOpen2)) {
int chooseOK = 0;
fileChooser.setCurrentDirectory(new File("."));//设置当前工作目录
fileChooser.setMultiSelectionEnabled(true);//可以选择多个文件
chooseOK = fileChooser.showOpenDialog(this);
if (chooseOK == 0) {
files = fileChooser.getSelectedFiles();
}
else {
return;
}
//size计算出选取文件的总大小
double size = 0;
for (int i=0; i<files.length; i++) {
size += files[i].length();
}
size = size / 1024.0;
size = Math.round(size * 100);
size = size / 100;
labelOpen2.setText("选取了:"+files.length+"个文件."
+"总大小为:"+ size + "KB");
//acount计算出组合文件进度条的最大值
//分别计算每个文件的组合所需要的运算次数
byte[] b = new byte[512];
for (int i=0; i<files.length; i++) {
acount += (int)(files[i].length()/b.length);
if (files[i].length()%b.length != 0) {
acount++;
}
}
progressBar2.setMaximum(acount);
}
//分割文件
if (event.getSource().equals(jbtSplit)) {
if (file == null) {
JOptionPane.showMessageDialog(this,"源文件不存在!","警告",JOptionPane.ERROR_MESSAGE);
return;
}
if (textSplit.getText().equals("")) {
JOptionPane.showMessageDialog(this,"没有设置分割份数!","警告",JOptionPane.ERROR_MESSAGE);
return;
}
int x = 0;
byte[] b = new byte[512];
/*分割份数(默认为1)
*如果填写4,splitAcount的值=3
*前三个文件的大小相同,最后一是剩余文件的大小
*/
int splitAcount = Integer.parseInt(textSplit.getText())-1;
if (splitAcount < 1) {
splitAcount = 1;
}
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fin = new FileInputStream(file);
//分割文件
int m=1;
//创建文件夹
new File("split").mkdir();
fout = new FileOutputStream("split/split_" + m + ".DAT");
for (int i=1; i<=acount; i++) {
x = fin.read(b,0,b.length);
if (i%(acount/splitAcount)==0) {
m++;
fout = new FileOutputStream("split/split_" + m + ".DAT");
}
fout.write(b,0,x);
progressBar.setValue(i);
}
fin.close();
fout.close();
JOptionPane.showMessageDialog(this,"文件分割成功!");
}
catch (IOException e) {e.printStackTrace();}
}
//组合文件
if (event.getSource().equals(jbtCompages)) {
if (files == null) {
JOptionPane.showMessageDialog(this,"没有选择待组合的文件!","警告",JOptionPane.ERROR_MESSAGE);
return;
}
int x = 0;
byte[] b = new byte[512];
FileInputStream fin = null;
FileOutputStream fout = null;
try {
fout = new FileOutputStream("compage.DAT");
for (int i=0; i<files.length; i++) {
fin = new FileInputStream(files[i]);
while ((x = fin.read(b,0,b.length)) != -1) {
fout.write(b,0,x);
progressBar2.setValue(progressBar2.getValue()+1);
}
fin.close();
}
fout.close();
JOptionPane.showMessageDialog(this,"文件组合成功!");
}
catch (IOException e) {e.printStackTrace();}
}
}
public static void main(String[] args) {
FileSplit split = new FileSplit();
split.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -