文件分割器

来源:互联网 发布:depthmap软件说明 编辑:程序博客网 时间:2024/06/02 11:41

先说说思路吧,就是根据所需要分得份数,使用RandomAccessFile移动指针到指定的位置,然后在分别得写入不同的文件…以此抛砖引玉,希望大家多指点…写的不是很完美吧…很多问题还没有考虑到…

Code:
  1. import java.awt.BorderLayout;   
  2. import java.awt.event.ActionEvent;   
  3. import java.awt.event.ActionListener;   
  4. import java.io.File;   
  5. import java.io.FileNotFoundException;   
  6. import java.io.FilenameFilter;   
  7. import java.io.IOException;   
  8. import java.io.RandomAccessFile;   
  9.   
  10. import javax.swing.JButton;   
  11. import javax.swing.JFileChooser;   
  12. import javax.swing.JFrame;   
  13. import javax.swing.JLabel;   
  14. import javax.swing.JOptionPane;   
  15. import javax.swing.JPanel;   
  16. import javax.swing.JTextField;   
  17.   
  18. /**  
  19.  * 合并功能  
  20.  * @author Xueqi  
  21.  *  
  22.  */  
  23. public class Combine extends JFrame {   
  24.     private JLabel jl1 = new JLabel("合并文件的位置: ");   
  25.     private JLabel jl2 = new JLabel("保存文件的位置: ");   
  26.     private JTextField t1 = new JTextField(40);   
  27.     private JTextField t2 = new JTextField(40);   
  28.     private JButton jb1 = new JButton("选择");   
  29.     private JButton jb2 = new JButton("选择");   
  30.     private JButton jb3 = new JButton("合并");   
  31.     private JPanel p1 = new JPanel();   
  32.     private JPanel p2 = new JPanel();   
  33.     private JPanel p3 = new JPanel();   
  34.     private File openF;//要合并的文件   
  35.     private String openPath;//要合并文件的路径   
  36.     private File saveF;//要保存到的文件夹   
  37.     private String savePath;//路径   
  38.     long end = 0;   
  39.     String saveName ;   
  40.        
  41.     public Combine() {   
  42.         super("FileSplitters 合并操作");   
  43.         t1.setEditable(false);   
  44.         t2.setEditable(false);   
  45.         p1.add(jl1);   
  46.         p1.add(t1);   
  47.         p1.add(jb1);   
  48.         p2.add(jl2);   
  49.         p2.add(t2);   
  50.         p2.add(jb2);   
  51.         p3.add(jb3);   
  52.         this.setResizable(false);   
  53.         this.setBounds(400300630156);   
  54.         this.add(p1,BorderLayout.NORTH);   
  55.         this.add(p2,BorderLayout.CENTER);   
  56.         this.add(p3,BorderLayout.SOUTH);   
  57.         this.setVisible(true);   
  58.            
  59.         open();   
  60.         save();   
  61.         combine();   
  62.     }   
  63.   
  64.     private void combine() {   
  65.         jb3.addActionListener(new ActionListener() {   
  66.             public void actionPerformed(ActionEvent e) {   
  67.                 com(openF);   
  68.                 JOptionPane.showMessageDialog(null"合并完成!/n保存到:" + savePath + "//" + saveName);   
  69.             }   
  70.         });   
  71.     }   
  72.   
  73.     private void save() {   
  74.         jb2.addActionListener(new ActionListener() {   
  75.             public void actionPerformed(ActionEvent e) {   
  76.                 JFileChooser chooser = new JFileChooser();   
  77.                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);   
  78.                 int val = chooser.showSaveDialog(null);   
  79.                 saveF = chooser.getSelectedFile();   
  80.                 if(val == JFileChooser.APPROVE_OPTION) {   
  81.                     savePath = saveF.getAbsolutePath();   
  82.                     t2.setText(savePath);   
  83.                 }   
  84.             }   
  85.         });   
  86.     }   
  87.   
  88.     private void open() {   
  89.         jb1.addActionListener(new ActionListener() {   
  90.             public void actionPerformed(ActionEvent e) {   
  91.                 JFileChooser chooser = new JFileChooser();   
  92.                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);   
  93.                 int val = chooser.showOpenDialog(null);   
  94.                 openF = chooser.getSelectedFile();   
  95.                 if(val == JFileChooser.APPROVE_OPTION) {   
  96.                     openPath = openF.getAbsolutePath();   
  97.                     t1.setText(openPath);   
  98.                 }   
  99.             }   
  100.         });   
  101.     }   
  102.   
  103.     /**  
  104.      *   
  105.      * @param openPath 要合并文件所在的文件夹  
  106.      * @param savePath 将合并的文件保存的位置  
  107.      */  
  108.     public void com(File openF) {   
  109.         try {   
  110.             long off = 0;   
  111.             RandomAccessFile in = null;   
  112.             File[] f = openF.listFiles(new FilenameFilter() {   
  113.                 public boolean accept(File dir, String name) {   
  114.                     if (name.endsWith(".tmp")) {   
  115.                         return true;   
  116.                     }   
  117.                     return false;   
  118.                 }   
  119.             });   
  120.             int num = f.length;// 获取分成了多少份   
  121. System.out.println(num);   
  122. for(File e :f) {   
  123.     System.out.println(e.getName());   
  124. }   
  125.             String[] saveTmp = f[0].getName().split("//.");   
  126.             saveName = saveTmp[0] + "." + saveTmp[1];   
  127.             for (int i = 0; i < num; i++) {   
  128.                 in = new RandomAccessFile(f[i], "r");   
  129.                 long begin = off;   
  130.                 off = work(saveName, f[i], savePath, begin);   
  131.                 in.close();   
  132.             }   
  133.         } catch (FileNotFoundException e) {   
  134.             e.printStackTrace();   
  135.         } catch (IOException e) {   
  136.             e.printStackTrace();   
  137.         }    
  138.     }   
  139.        
  140.     public long work(String saveName, File f, String savePath, long begin) {   
  141.         try {   
  142.             RandomAccessFile in = new RandomAccessFile(f, "r");   
  143.             RandomAccessFile out = new RandomAccessFile(savePath + "//" + saveName, "rw");   
  144.             end += in.length();//获取长度   
  145.             out.seek(begin);//写入文件指针的偏移   
  146.             byte[] buff = new byte[1024];   
  147.             int read = 0;   
  148.             while((read = in.read(buff)) != -1) {   
  149.                 out.write(buff, 0, read);   
  150.             }   
  151.             in.close();   
  152.             out.close();   
  153.         } catch (FileNotFoundException e) {   
  154.             e.printStackTrace();   
  155.         } catch (IOException e) {   
  156.             e.printStackTrace();   
  157.         }   
  158.         return end;   
  159.     }   
  160. }   

 

 

 

Code:
  1. import java.awt.BorderLayout;   
  2. import java.awt.event.ActionEvent;   
  3. import java.awt.event.ActionListener;   
  4. import java.io.File;   
  5. import java.io.FileNotFoundException;   
  6. import java.io.FileOutputStream;   
  7. import java.io.IOException;   
  8. import java.io.RandomAccessFile;   
  9.   
  10. import javax.swing.JButton;   
  11. import javax.swing.JFileChooser;   
  12. import javax.swing.JFrame;   
  13. import javax.swing.JLabel;   
  14. import javax.swing.JOptionPane;   
  15. import javax.swing.JPanel;   
  16. import javax.swing.JTextField;   
  17.   
  18. /**  
  19.  * 分割功能  
  20.  * @author Xueqi  
  21.  *  
  22.  */  
  23. public class Partition extends JFrame {   
  24.     private JLabel jl1 = new JLabel("分割文件的位置: ");   
  25.     private JLabel jl2 = new JLabel("分割文件的份数: ");   
  26.     private JLabel jl3 = new JLabel("保存文件的位置: ");   
  27.     private JButton jb1 = new JButton("选择");   
  28.     private JButton jb2 = new JButton("选择");   
  29.     private JButton jb3 = new JButton("分割");   
  30.     private JTextField t1 = new JTextField(40);   
  31.     private JTextField t2 = new JTextField(5);   
  32.     private JTextField t3 = new JTextField(40);   
  33.     private JPanel p1 = new JPanel();   
  34.     private JPanel p2 = new JPanel();   
  35.     private JPanel p3 = new JPanel();   
  36.        
  37.     private long length = 0;;//文件的长度   
  38.     private int num = 0;//份数   
  39.     private File openF;//要分割的文件   
  40.     private String openPath;//要分割文件的路径   
  41.     private String openFileName;   
  42.     private File saveF;//要保存到的文件夹   
  43.     private String savePath;//路径   
  44.        
  45.     public Partition() {   
  46.         super("FileSplitters 分割操作");   
  47.         t1.setEditable(false);   
  48.         t3.setEditable(false);   
  49.         p1.add(jl1);   
  50.         p1.add(t1);   
  51.         p1.add(jb1);   
  52.         p2.add(jl3);   
  53.         p2.add(t3);   
  54.         p2.add(jb2);   
  55.         p3.add(jl2);   
  56.         p3.add(t2);   
  57.         p3.add(new JLabel("      "));   
  58.         p3.add(jb3);   
  59.         this.setResizable(false);   
  60.         this.setBounds(400300630156);   
  61.         this.add(p1,BorderLayout.NORTH);   
  62.         this.add(p2,BorderLayout.CENTER);   
  63.         this.add(p3,BorderLayout.SOUTH);   
  64.         this.setVisible(true);   
  65.            
  66.         chooser();   
  67.         save();   
  68.         splitters();   
  69.     }   
  70.   
  71.     private void splitters() {   
  72.         jb3.addActionListener(new ActionListener() {   
  73.             public void actionPerformed(ActionEvent e) {   
  74.                 num = Integer.parseInt(t2.getText());   
  75.                 work(num);   
  76.                 JOptionPane.showMessageDialog(null"分割完成!/n保存到:" + savePath + " /n 共" + num + "份");   
  77.             }   
  78.         });   
  79.     }   
  80.   
  81.     private void save() {   
  82.         jb2.addActionListener(new ActionListener() {   
  83.             public void actionPerformed(ActionEvent e) {   
  84.                 JFileChooser chooser = new JFileChooser();   
  85.                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);   
  86.                 int val = chooser.showSaveDialog(null);   
  87.                 saveF = chooser.getSelectedFile();   
  88.                 if(val == JFileChooser.APPROVE_OPTION) {/////////////////////////   
  89.                     savePath = saveF.toString();   
  90.                     t3.setText(savePath);   
  91.                 }   
  92.             }   
  93.         });   
  94.     }   
  95.   
  96.     private void chooser() {   
  97.         jb1.addActionListener(new ActionListener() {   
  98.             public void actionPerformed(ActionEvent e) {   
  99.                 JFileChooser chooser = new JFileChooser();   
  100.                 int val = chooser.showOpenDialog(null);   
  101.                 openF = chooser.getSelectedFile();   
  102.                 if(val == JFileChooser.OPEN_DIALOG) {   
  103.                     openPath = openF.getAbsolutePath();   
  104.                     t1.setText(openPath);   
  105.                     openFileName = openF.getName();   
  106.                 }   
  107.             }   
  108.         });   
  109.     }   
  110.   
  111.     public void work(int num) {   
  112.         try {   
  113.             RandomAccessFile raf = new RandomAccessFile(openPath, "r");   
  114.             length = raf.length();   
  115.             raf.close();   
  116.             long size = length/num;//每份文件的大小   
  117.             long endPoint = 0;//读取结束时的位置   
  118.             //先解决最后一个之前的   
  119.             for(int i = 0; i< num - 1; i++) {   
  120.                 //结束位置是下一个的起始位置   
  121.                 long begin = endPoint;//开始位置   
  122.                 long endLength = (i+1) * size;//读取到的位置   
  123.                 endPoint = spilt(openPath,savePath,i,begin,endLength);//得到末尾位置   
  124.             }   
  125.             //解决最后一份   
  126.             if(length - endPoint >0) {   
  127.                 spilt(openPath,savePath,num - 1,endPoint,length);   
  128.             }          
  129.         } catch (FileNotFoundException e) {   
  130.             e.printStackTrace();   
  131.         } catch (IOException e) {   
  132.             e.printStackTrace();   
  133.         }   
  134.     }   
  135.        
  136.     /**  
  137.      *   
  138.      * @param openFile 分割文件的路径  
  139.      * @param savefile 保存文件的路径  
  140.      * @param index 第几号文件  
  141.      * @param begin 指针的起始位置  
  142.      * @param end 读取到的位置  
  143.      */  
  144.     public long spilt(String openFile, String saveFile, int index, long begin, long end) {//分割方法   
  145.         long point = 0;   
  146.         try {   
  147.             RandomAccessFile in = new RandomAccessFile(openFile, "r");   
  148.             RandomAccessFile out = new RandomAccessFile(saveFile + "//" + openFileName + "." + index + ".tmp", "rw");   
  149.             byte[] buuf = new byte[1024];   
  150.             int read = 0;   
  151.             //读取文件移动的指针位置   
  152.             in.seek(begin);   
  153.             while((in.getFilePointer() <= end) && (read = in.read(buuf)) != -1) {   
  154.                 out.write(buuf, 0, read);   
  155.             }   
  156.             point = in.getFilePointer();   
  157.             in.close();   
  158.             out.close();   
  159.         } catch (FileNotFoundException e) {   
  160.             e.printStackTrace();   
  161.         } catch (IOException e) {   
  162.             e.printStackTrace();   
  163.         }   
  164.            
  165.         return point;   
  166.     }   
  167. }