文件分割

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.IOException;
 4 import java.io.RandomAccessFile;
 5 
 6 public class FileSplitDemo {
 7      public static void main(String[] args) throws IOException {
 8          //分好块以后,调用方法读取每一块
 9          File f=new File("src/com/hh/ThreadDemoe.java");
10          long length=f.length();//文件大小
11          int blockSize=60;//块大小
12          int blockCount=(int)Math.ceil(length*1.0/blockSize);//块数
13          int beginPos=0;
14          int readSize=0;
15          for(int i=0;i<blockCount;i++){
16              beginPos=i*blockSize;
17              if(i<blockCount-1){
18                  readSize=blockSize;
19              }else{
20                  readSize=(int)length%blockSize;
21              }
22              split(i,beginPos,readSize);
23          }
24     }
25      /*
26       * 块数,起始位置,读取长度
27       */
28     private static void split(int i,int beginPos,int actualSize) throws IOException {
29         RandomAccessFile ras=new RandomAccessFile("src/com/hh/ThreadDemoe.java","r");
30         
31          ras.seek(beginPos);//随机读取
32          //读取操作
33          byte[] bys=new byte[10];
34          int len=-1;//接收长度
35          while((len=ras.read(bys))!=-1){
36              
37              if(actualSize>len){//剩余长度大于接收长度
38                  System.out.print(new String(bys,0,len));
39                  actualSize-=len;
40              }else{//剩余长度小于接收长度
41                 System.out.print(new String(bys,0,actualSize)); 
42                 break;
43              }
44          }
45          ras.close();
46     }
}
 1     //将分块和读取放在一起,读取全部文件
 2     private static void readFileSplit() throws FileNotFoundException, IOException {
 3         RandomAccessFile ras=new RandomAccessFile("src/hh/ThreadDemoe.java","r");
 4          long len=ras.length();//文件长度
 5          int blockSize=50;//块大小
 6          int blockNum=(int) Math.ceil((len*1.0)/blockSize);//块数
 7          
 8          byte[] bys=new byte[blockSize];//缓冲数组大小使用块大小
 9          for(int i=0;i<blockNum;i++){
10              ras.seek(i*blockSize);
11              if(i<blockNum-1){
12                  ras.read(bys);
13                  System.out.print(new String(bys,0,blockSize));
14              }else {
15                  int size=ras.read(bys);//文件的最后一块,直接读取返回大小
16                  System.out.print(new String(bys,0,size));
17              }
18          }
19         ras.close();
20     }

 

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.io.IOException;
 4 import java.io.RandomAccessFile;
 5 
 6 public class FileSplitDemo {
 7      public static void main(String[] args) throws IOException {
 8          //分好块以后,调用方法读取每一块
 9          File f=new File("src/com/hh/ThreadDemoe.java");
10          long length=f.length();//文件大小
11          int blockSize=60;//块大小
12          int blockCount=(int)Math.ceil(length*1.0/blockSize);//块数
13          int beginPos=0;
14          int readSize=0;
15          for(int i=0;i<blockCount;i++){
16              beginPos=i*blockSize;
17              if(i<blockCount-1){
18                  readSize=blockSize;
19              }else{
20                  readSize=(int)length%blockSize;
21              }
22              split(i,beginPos,readSize);
23          }
24     }
25      /*
26       * 块数,起始位置,读取长度
27       * 
28       * 跟字节流一样使,就是读写用的一个类new出来的对象
29       */
30     private static void split(int i,int beginPos,int actualSize) throws IOException {
31         RandomAccessFile ras=new RandomAccessFile("src/com/hh/ThreadDemoe.java","r");
32         RandomAccessFile ras2=new RandomAccessFile("ff/"+i+"Copy","rw");
33          ras.seek(beginPos);//随机读取
34          //读取操作
35          byte[] bys=new byte[10];
36          int len=-1;//接收长度
37          while((len=ras.read(bys))!=-1){
38              
39              if(actualSize>len){//剩余长度大于接收长度
40                  ras2.write(bys,0,len);
41                  actualSize-=len;
42              }else{//剩余长度小于接收长度
43                  ras2.write(bys,0,actualSize);
44                 break;
45              }
46          }
47          ras.close();
48     }
49 }

 --------------------------------------------------------------------------------------------------

使用面向对象思想进行封装

  1 import java.io.BufferedInputStream;
  2 import java.io.File;
  3 import java.io.FileInputStream;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 import java.io.RandomAccessFile;
  9 import java.io.SequenceInputStream;
 10 import java.util.ArrayList;
 11 import java.util.Vector;
 12 
 13 public class FileSplitDemo {
 14     private File f;
 15     private String dest;//父文件夹
 16     private ArrayList<String> list=new ArrayList<String>();
 17     private int blockSize;
 18     private long length;//文件大小
 19     private int blockCount;//块数
 20      public FileSplitDemo(String srcStr, String dest, int blockSize) {
 21         super();
 22         this.f = new File(srcStr);
 23         this.dest = dest;
 24         this.blockSize = blockSize;
 25         init();
 26     }
 27     private void init(){
 28         length=f.length();
 29         blockCount=(int)Math.ceil(length*1.0/blockSize);
 30         for(int i=0;i<blockCount;i++){
 31             list.add(dest+"/"+i+"-"+this.f.getName());
 32             System.out.println(list.get(i));
 33         }
 34         
 35     } 
 36      
 37      
 38      
 39     public  void split1() throws IOException {
 40          //分好块以后,调用方法读取每一块
 41          int beginPos=0;
 42          int readSize=0;
 43          for(int i=0;i<blockCount;i++){
 44              beginPos=i*blockSize;
 45              if(i<blockCount-1){
 46                  readSize=blockSize;
 47              }else{
 48                  readSize=(int)length%blockSize;
 49              }
 50              split(i,beginPos,readSize);
 51          }
 52     }
 53      /*
 54       * 块数,起始位置,读取长度
 55       * 
 56       * 跟字节流一样使,就是读写用的一个类new出来的对象
 57       */
 58     private  void split(int i,int beginPos,int actualSize) throws IOException {
 59         RandomAccessFile ras=new RandomAccessFile(f,"r");
 60         RandomAccessFile ras2=new RandomAccessFile(list.get(i),"rw");
 61          ras.seek(beginPos);//随机读取
 62          //读取操作
 63          byte[] bys=new byte[10];
 64          int len=-1;//接收长度
 65          while((len=ras.read(bys))!=-1){
 66              
 67              if(actualSize>len){//剩余长度大于接收长度
 68                  ras2.write(bys,0,len);
 69                  actualSize-=len;
 70              }else{//剩余长度小于接收长度
 71                  ras2.write(bys,0,actualSize);
 72                 break;
 73              }
 74          }
 75          ras.close();
 76     }
 77     //将多个文件合并到一个文件中,使用多个输入流和一个输出流
 78     private void merge() throws IOException{
 79         OutputStream os=new FileOutputStream("ff/merge.txt",true);
 80         byte[] bys=new byte[1024];
 81         int len;
 82         for(int i=0;i<blockCount;i++){
 83             InputStream is=new FileInputStream(list.get(i));
 84             while((len=is.read(bys))!=-1){
 85                 os.write(bys,0,len);
 86             }
 87             is.close();
 88         }
 89         os.close();
 90     }
 91     //将多个文件合并到一个文件中,使用序列流和一个输出流
 92     private void merge2() throws IOException{
 93         OutputStream os=new FileOutputStream("ff/merge.txt",true);
 94         Vector<InputStream> vi=new Vector<InputStream>();//集合对象
 95         for(int i=0;i<blockCount;i++){
 96             vi.add(new BufferedInputStream(new FileInputStream(list.get(i))));
 97         }
 98         SequenceInputStream sis=null;//序列流
 99         sis=new SequenceInputStream(vi.elements());
100         
101         byte[] bys=new byte[1024];
102         int len;
103         while((len=sis.read(bys))!=-1){
104             os.write(bys,0,len);
105         }
106         sis.close();
107         os.close();
108     }
109     
110     
111     //测试
112     public static void main(String[] args) throws IOException {
113         FileSplitDemo fsd=new FileSplitDemo("src/com/hh/ThreadDemoe.java", "ff", 50);
114         fsd.split1();
115         //fsd.merge();
116         fsd.merge2();
117     }
118 }

 

posted @ 2020-02-13 17:19  /*nobody*/  阅读(169)  评论(0编辑  收藏  举报