package day20_work1.tarena.com;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
* 使用文件缓冲区的文件分割与合并
* */
public class FilePartitionNio implements ActionListener {
private JTextField jt1;
private JTextField jt2;
public FilePartitionNio() {
JFrame jf = new JFrame("我的文件分割器");
jf.setLayout(new FlowLayout());
JLabel jl1 = new JLabel("文件");
JLabel jl2 = new JLabel("大小");
jt1 = new JTextField(20);
jt2 = new JTextField(15);
JButton jb1 = new JButton("打开");
JButton jb2 = new JButton("分割");
JButton jb3 = new JButton("合并");
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jf.add(jl1);
jf.add(jt1);
jf.add(jb1);
jf.add(jl2);
jf.add(jt2);
jf.add(jb2);
jf.add(jb3);
jf.setSize(350, 100);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void Partition(String path, int num) { // 拆分文件
int i = 0;
try {
File f = new File(path);
FileInputStream fis = new FileInputStream(f);
FileChannel fc1 = fis.getChannel();//读文件通道
ByteBuffer b = ByteBuffer.allocate(num*1024);
while (true) {
int temp = fc1.read(b);
if(temp == -1) break;
FileOutputStream fos = new FileOutputStream(path + i);
FileChannel fc2 = fos.getChannel();//写文件通道
b.flip();//限制设置为当前位置,位置设置为0
fc2.write(b);
b.clear();//不清空,限制设为容量,位置设为position
i++;
System.out.println(temp);
fc2.close();
fos.close();
}
fc1.close();
f.delete();
System.out.println("拆分文件完毕");
fis.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
public void unite(String path){ // 合并文件
int i = 0;
try{
FileOutputStream fos = new FileOutputStream(path,true);
FileChannel fc2 = fos.getChannel();//写文件通道
while(true){
File f = new File(path+i);
if(f.exists()){
FileInputStream fis = new FileInputStream(f);
FileChannel fc1 = fis.getChannel();//读文件通道
ByteBuffer b = ByteBuffer.allocate(1024);
while (true) {
int te = fc1.read(b);
if (te == -1) break;
b.flip();//限制设置为当前位置,位置设置为0
fc2.write(b);
b.clear();//不清空,限制设为容量,位置设为position
}
fc1.close();
fis.close();
}else{
System.out.println("合并文件完毕");
break;
}
f.delete();
i++;
}
fc2.close();
fos.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if ("打开".equals(comm)) {
try {
JFileChooser fc = new JFileChooser();
fc.showOpenDialog(null);
jt1.setText(fc.getSelectedFile().getPath());
} catch (Exception ee) {
ee.printStackTrace();
}
} else if ("分割".equals(comm)) {
if(!jt1.getText().equals("") && !jt2.getText().equals("")){
Partition(jt1.getText(), Integer.parseInt(jt2.getText()));
}else{
System.out.println("请输入文件路径");
}
} else if ("合并".equals(comm)) {
if(!jt1.getText().equals("")){
unite(jt1.getText());
}else{
System.out.println("缺少文件名");
}
}
}
public static void main(String[] args) {
new FilePartitionNio();
}
}