文件切割跟文件合并
文件切割和文件合并
文件合并:

本软件采用图形界面,事件监听,文件类IO流读写文件,序列流,文件拷贝,背景设置等一系列技术。通过对字节的拷贝和对流的操作来实现文件的切割和合并功能。
注意:由于切割后的文件名是按我个人习惯命名的,可在代码中自行更改,格式不变;合并之后的文件名也是本人命名,可自行更改,文件格式需要根据用户自己要求自行选择(格式为:.XXXX)。
以下是软件代码和详细说明:
import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class FlieSplit extends JFrame implements ActionListener { public static void main(String[] args) { new FlieSplit("文件切割与合并"); } // 拆分所用的组件 JLabel JLOpen = null; JLabel JLSave = null; JTextField JTOpen = null; JTextField JTSave = null; JButton JBChooseOpen = null; JButton JBChooseSave = null; JButton JBStartSplit = null; // 合并所用的组件 JLabel JLCombineOpen = null; JLabel JLPostfixStr = null; JTextField JTCombineOpen = null; JTextField JTPostfixSt = null; JButton JBCombineOpen = null; JButton JBStartCombine = null; JButton JBExit = null; // 文件操作所用 boolean flag1 = false; boolean flag2 = false; boolean flag3 = false; int MaxSize = 1024 * 1024; File file = null; File desDir = null;// 目录文件 File[] files = null; // 文件数组 String desStr = null; String PostfixStr = null; // 文件后缀 String strDir = "./image/";//背景图片位置 // 构造函数 public FlieSplit(String str) { super(str); this.setBounds(400, 100, 350, 420);// 设置位置 this.getContentPane().setLayout(null); // 流动布局设置为空 this.setDefaultCloseOperation(EXIT_ON_CLOSE); setBack();// 设置背景 initialize(); // 初始化界面 this.setVisible(true); } private void initialize() { /* * 拆分界面设置 */ // 标签设置 JLOpen = new JLabel("选择需要拆分的文件:"); JLOpen.setBounds(50, 10, 150, 20); this.getContentPane().add(JLOpen); // 文本框设置 JTOpen = new JTextField(); JTOpen.setBounds(20, 40, 235, 30); JTOpen.setEditable(false); this.getContentPane().add(JTOpen); // 按钮设置 JBChooseOpen = new JButton("打开"); JBChooseOpen.setBounds(260, 40, 70, 30); this.getContentPane().add(JBChooseOpen); JBChooseOpen.addActionListener(this); JBStartSplit = new JButton("开始分割"); JBStartSplit.setBounds(90, 80, 150, 30); JBStartSplit.setEnabled(false); this.getContentPane().add(JBStartSplit); JBStartSplit.addActionListener(this); /* * 保存目录设置 */ // 标签设置 JLSave = new JLabel("选择需要存放的文件目录:"); JLSave.setBounds(50, 230, 180, 20); this.getContentPane().add(JLSave); // 文本框设置 JTSave = new JTextField(); JTSave.setBounds(20, 260, 230, 30); JTSave.setEditable(false); this.getContentPane().add(JTSave); // 按钮设置 JBChooseSave = new JButton("另存为"); JBChooseSave.setBounds(255, 260, 75, 30); this.getContentPane().add(JBChooseSave); JBChooseSave.addActionListener(this); /* * 合并界面设置 */ // 标签设置 JLCombineOpen = new JLabel("选择需要合并的文件:"); JLCombineOpen.setBounds(50, 120, 200, 20); this.getContentPane().add(JLCombineOpen); JLPostfixStr = new JLabel("文件格式:"); JLPostfixStr.setBounds(20, 190, 80, 30); this.getContentPane().add(JLPostfixStr); // 文本框设置 JTCombineOpen = new JTextField(); JTCombineOpen.setBounds(20, 150, 235, 30); JTCombineOpen.setEditable(false); this.getContentPane().add(JTCombineOpen); JTPostfixSt = new JTextField(""); JTPostfixSt.setBounds(85, 190, 80, 30); this.getContentPane().add(JTPostfixSt); // 按钮设置 JBCombineOpen = new JButton("打开"); JBCombineOpen.setBounds(260, 150, 70, 30); this.getContentPane().add(JBCombineOpen); JBCombineOpen.addActionListener(this); JBStartCombine = new JButton("开始合并"); JBStartCombine.setBounds(190, 190, 120, 30); JBStartCombine.setEnabled(false); this.getContentPane().add(JBStartCombine); JBStartCombine.addActionListener(this); JBExit = new JButton("退出"); JBExit.setBounds(125, 320, 80, 30); this.getContentPane().add(JBExit); JBExit.addActionListener(this); } // 监听 @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(JBExit)) { // 退出程序 System.exit(0); } if (e.getSource().equals(JBChooseOpen)) { // 选择所需要拆分的文件 flag1 = false; ChooseOpen(); } if (e.getSource().equals(JBChooseSave)) { // 选择拆分后保存的文件目录 flag2 = false; ChooseSave(); } if (e.getSource().equals(JBStartSplit)) { // 开始拆分 StartSplit(); } if (e.getSource().equals(JBCombineOpen)) { // 选择所需要合并的文件 flag3 = false; CombineOpen(); } if (e.getSource().equals(JBStartCombine)) { // 开始合并 StartCombine(); } // 如果没有选择文件和目录的时候开始按钮设置为不能操作 if (flag1 && flag2) { JBStartSplit.setEnabled(true); } if (flag2 && flag3) { JBStartCombine.setEnabled(true); } } // 选择所需要拆分的文件 private void ChooseOpen() { JFileChooser JFChoose = new JFileChooser(); int result = JFChoose.showOpenDialog(this); // 获取需要切割的文件的文件名 if (result == JFileChooser.APPROVE_OPTION) { file = JFChoose.getSelectedFile();// 用户所选择的文件 JTOpen.setText(file.getAbsolutePath()); // System.out.println(file.getName()); PostfixStr = getPostfixStr(file);// 获取用户所选文件的格式 flag1=true; } } // 选择拆分后保存的文件目录 private void ChooseSave() { JFileChooser JFChooseSave = new JFileChooser(); JFChooseSave.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); int result = JFChooseSave.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { desDir = JFChooseSave.getSelectedFile().getAbsoluteFile(); JTSave.setText(desDir.getAbsolutePath()); // 把文件的路径写到文本框中 // desDir=new File(JFCombine.getSelectedFile().getParent()); // System.out.println(desDir.getName()); flag2=true; } } // 开始拆分 private void StartSplit() { // 源文件 try { FileInputStream fis = new FileInputStream(file); // 文件io流操作的健壮性防护(用File对象去开道) // if(!desDir.exists()){ // desDir.mkdirs(); // } // 切割 FileOutputStream fos = null; byte buf[] = new byte[MaxSize]; int len = 0; int count = 1; while ((len = fis.read(buf)) != -1) {// buf最后一个字符默认为-1 String fileName = "XHSplit" + (count++) + PostfixStr; fos = new FileOutputStream(new File(desDir, fileName)); fos.write(buf, 0, len); // 拷贝 fos.close(); } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "文件没有找到!!!"); return; } catch (IOException e) { JOptionPane.showMessageDialog(this, "切割失败!!"); return; } JOptionPane.showMessageDialog(this, "切割成功!!"); } // 选择所需要合并的文件 private void CombineOpen() { JFileChooser JFCombine = new JFileChooser(); JFCombine.setMultiSelectionEnabled(true);// 把文件选择器设置成可以选多个文件的模式 int result = JFCombine.showOpenDialog(this); String str = "";// 把选择的文件的文件名放在对应的文本框显示 if (result == JFileChooser.APPROVE_OPTION) { files = JFCombine.getSelectedFiles();// 把需要合并的文件取出来放在文件数组中 for (int i = 0; i < files.length; i++) { str = str + files[i].getName() + ";"; } JTCombineOpen.setText(str);// 把选择的文件的文件名放在对应的文本框显示 } flag3=true; } // 开始合并 private void StartCombine() { PostfixStr = JTPostfixSt.getText(); while (PostfixStr.equals("")) { JOptionPane.showMessageDialog(this, "请选择需要合并的文件的格式!"); return; } if (files.length == 0) { JOptionPane.showMessageDialog(this, "所需要合并的碎片不存在!!"); return; } try { // 用序列流进行文件合并 String fileName = "XHCombine" + PostfixStr; ArrayList<FileInputStream> aList = new ArrayList<FileInputStream>(); for (int i = 0; i < files.length; i++) { aList.add(new FileInputStream(new File(files[i].getParent(), files[i] .getName()))); } // 枚举接口对象 Enumeration<FileInputStream> en = Collections.enumeration(aList); SequenceInputStream sis = new SequenceInputStream(en); // 把序列流当中的内容写到用户指定的文件目录中 FileOutputStream fos = new FileOutputStream(new File(desDir, fileName)); byte buf[] = new byte[1024 * 1024]; int len = 0; while ((len = sis.read(buf)) != -1) { fos.write(buf, 0, len); } fos.close(); sis.close(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(this, "文件没有找到!!!"); return; } catch (IOException e) { throw new RuntimeException("合并失败!!!"); } JOptionPane.showMessageDialog(this, "合并成功!!"); } // 获取用户所选文件的格式 private String getPostfixStr(File file2) { /* * 从文件名的最后开始找,找到第一个'.',然后从这里开始到最后就是文件的格式 */ String str = file2.getName(); int i; char[] ch = str.toCharArray(); for (i = ch.length - 1; i >= 0; i--) { if (ch[i] == '.') {// 找到位置,直接退出循环 break; } } return str.substring(i, str.length());// 从文件名中截取字符串,即文件的格式 } // 设置背景 private void setBack() { ((JPanel) (this.getContentPane())).setOpaque(false);// 使getContentPane()透明 ImageIcon bgImage = new ImageIcon(strDir + "photo.png"); JLabel bgLabel = new JLabel(bgImage); bgLabel.setBounds(0, 0, bgImage.getIconWidth(), bgImage.getIconHeight()); this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));// 这里需要把背景图片的优先级调到最低,才能使别的图片在其上面显示 } }
下面是本人自己运行的结果截图:
软件界面:
文件切割:
切割成功:
文件合并:
合并成功:
下面是软件的背景图:
版权声明:本文为博主原创文章,未经博主允许不得转载。