先说说思路吧,就是根据所需要分得份数,使用RandomAccessFile移动指针到指定的位置,然后在分别得写入不同的文件…以此抛砖引玉,希望大家多指点…写的不是很完美吧…很多问题还没有考虑到…
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- 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;
- /**
- * 合并功能
- * @author Xueqi
- *
- */
- public class Combine extends JFrame {
- private JLabel jl1 = new JLabel("合并文件的位置: ");
- private JLabel jl2 = new JLabel("保存文件的位置: ");
- private JTextField t1 = new JTextField(40);
- private JTextField t2 = new JTextField(40);
- private JButton jb1 = new JButton("选择");
- private JButton jb2 = new JButton("选择");
- private JButton jb3 = new JButton("合并");
- private JPanel p1 = new JPanel();
- private JPanel p2 = new JPanel();
- private JPanel p3 = new JPanel();
- private File openF;//要合并的文件
- private String openPath;//要合并文件的路径
- private File saveF;//要保存到的文件夹
- private String savePath;//路径
- long end = 0;
- String saveName ;
- public Combine() {
- super("FileSplitters 合并操作");
- t1.setEditable(false);
- t2.setEditable(false);
- p1.add(jl1);
- p1.add(t1);
- p1.add(jb1);
- p2.add(jl2);
- p2.add(t2);
- p2.add(jb2);
- p3.add(jb3);
- this.setResizable(false);
- this.setBounds(400, 300, 630, 156);
- this.add(p1,BorderLayout.NORTH);
- this.add(p2,BorderLayout.CENTER);
- this.add(p3,BorderLayout.SOUTH);
- this.setVisible(true);
- open();
- save();
- combine();
- }
- private void combine() {
- jb3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- com(openF);
- JOptionPane.showMessageDialog(null, "合并完成!/n保存到:" + savePath + "//" + saveName);
- }
- });
- }
- private void save() {
- jb2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int val = chooser.showSaveDialog(null);
- saveF = chooser.getSelectedFile();
- if(val == JFileChooser.APPROVE_OPTION) {
- savePath = saveF.getAbsolutePath();
- t2.setText(savePath);
- }
- }
- });
- }
- private void open() {
- jb1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int val = chooser.showOpenDialog(null);
- openF = chooser.getSelectedFile();
- if(val == JFileChooser.APPROVE_OPTION) {
- openPath = openF.getAbsolutePath();
- t1.setText(openPath);
- }
- }
- });
- }
- /**
- *
- * @param openPath 要合并文件所在的文件夹
- * @param savePath 将合并的文件保存的位置
- */
- public void com(File openF) {
- try {
- long off = 0;
- RandomAccessFile in = null;
- File[] f = openF.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- if (name.endsWith(".tmp")) {
- return true;
- }
- return false;
- }
- });
- int num = f.length;// 获取分成了多少份
- System.out.println(num);
- for(File e :f) {
- System.out.println(e.getName());
- }
- String[] saveTmp = f[0].getName().split("//.");
- saveName = saveTmp[0] + "." + saveTmp[1];
- for (int i = 0; i < num; i++) {
- in = new RandomAccessFile(f[i], "r");
- long begin = off;
- off = work(saveName, f[i], savePath, begin);
- in.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public long work(String saveName, File f, String savePath, long begin) {
- try {
- RandomAccessFile in = new RandomAccessFile(f, "r");
- RandomAccessFile out = new RandomAccessFile(savePath + "//" + saveName, "rw");
- end += in.length();//获取长度
- out.seek(begin);//写入文件指针的偏移
- byte[] buff = new byte[1024];
- int read = 0;
- while((read = in.read(buff)) != -1) {
- out.write(buff, 0, read);
- }
- in.close();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return end;
- }
- }
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- 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;
- /**
- * 分割功能
- * @author Xueqi
- *
- */
- public class Partition extends JFrame {
- private JLabel jl1 = new JLabel("分割文件的位置: ");
- private JLabel jl2 = new JLabel("分割文件的份数: ");
- private JLabel jl3 = new JLabel("保存文件的位置: ");
- private JButton jb1 = new JButton("选择");
- private JButton jb2 = new JButton("选择");
- private JButton jb3 = new JButton("分割");
- private JTextField t1 = new JTextField(40);
- private JTextField t2 = new JTextField(5);
- private JTextField t3 = new JTextField(40);
- private JPanel p1 = new JPanel();
- private JPanel p2 = new JPanel();
- private JPanel p3 = new JPanel();
- private long length = 0;;//文件的长度
- private int num = 0;//份数
- private File openF;//要分割的文件
- private String openPath;//要分割文件的路径
- private String openFileName;
- private File saveF;//要保存到的文件夹
- private String savePath;//路径
- public Partition() {
- super("FileSplitters 分割操作");
- t1.setEditable(false);
- t3.setEditable(false);
- p1.add(jl1);
- p1.add(t1);
- p1.add(jb1);
- p2.add(jl3);
- p2.add(t3);
- p2.add(jb2);
- p3.add(jl2);
- p3.add(t2);
- p3.add(new JLabel(" "));
- p3.add(jb3);
- this.setResizable(false);
- this.setBounds(400, 300, 630, 156);
- this.add(p1,BorderLayout.NORTH);
- this.add(p2,BorderLayout.CENTER);
- this.add(p3,BorderLayout.SOUTH);
- this.setVisible(true);
- chooser();
- save();
- splitters();
- }
- private void splitters() {
- jb3.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- num = Integer.parseInt(t2.getText());
- work(num);
- JOptionPane.showMessageDialog(null, "分割完成!/n保存到:" + savePath + " /n 共" + num + "份");
- }
- });
- }
- private void save() {
- jb2.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int val = chooser.showSaveDialog(null);
- saveF = chooser.getSelectedFile();
- if(val == JFileChooser.APPROVE_OPTION) {/
- savePath = saveF.toString();
- t3.setText(savePath);
- }
- }
- });
- }
- private void chooser() {
- jb1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- JFileChooser chooser = new JFileChooser();
- int val = chooser.showOpenDialog(null);
- openF = chooser.getSelectedFile();
- if(val == JFileChooser.OPEN_DIALOG) {
- openPath = openF.getAbsolutePath();
- t1.setText(openPath);
- openFileName = openF.getName();
- }
- }
- });
- }
- public void work(int num) {
- try {
- RandomAccessFile raf = new RandomAccessFile(openPath, "r");
- length = raf.length();
- raf.close();
- long size = length/num;//每份文件的大小
- long endPoint = 0;//读取结束时的位置
- //先解决最后一个之前的
- for(int i = 0; i< num - 1; i++) {
- //结束位置是下一个的起始位置
- long begin = endPoint;//开始位置
- long endLength = (i+1) * size;//读取到的位置
- endPoint = spilt(openPath,savePath,i,begin,endLength);//得到末尾位置
- }
- //解决最后一份
- if(length - endPoint >0) {
- spilt(openPath,savePath,num - 1,endPoint,length);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- *
- * @param openFile 分割文件的路径
- * @param savefile 保存文件的路径
- * @param index 第几号文件
- * @param begin 指针的起始位置
- * @param end 读取到的位置
- */
- public long spilt(String openFile, String saveFile, int index, long begin, long end) {//分割方法
- long point = 0;
- try {
- RandomAccessFile in = new RandomAccessFile(openFile, "r");
- RandomAccessFile out = new RandomAccessFile(saveFile + "//" + openFileName + "." + index + ".tmp", "rw");
- byte[] buuf = new byte[1024];
- int read = 0;
- //读取文件移动的指针位置
- in.seek(begin);
- while((in.getFilePointer() <= end) && (read = in.read(buuf)) != -1) {
- out.write(buuf, 0, read);
- }
- point = in.getFilePointer();
- in.close();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return point;
- }
- }