RandomAccessFile 不破坏文件的切割!

代码如下:

package com.tz.poker;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/** 
 * 类和方法的注释用javadoc
 * @author Administrator
 * 方法是行为 属性 年纪 肤色
 *
 */
public class SplitFileo {
      public static void main(String[] args) {
        getSplitFile("C:/D/one.jpg4", 10);
        
        System.out.println("完成");
    }

    //file 文件  count 要切割的分数
    public static void getSplitFile(String file,int count){
        //获取目标文件 预分配文件占用的空间 在磁盘中创建一个指定大小的文件(你要去吃饭 要有桌子 java 里面开辟的空间单独处理文件)
        // r 读  rw 读写 w 写
        //异常 编译时异常 运行时异常 区别:编译时底层封装 往上级抛出的 
        //运行时  底层 空指针异常 有一个判断if(xxx=null){}  
        RandomAccessFile raf = null;
            try {
                 raf = new RandomAccessFile(file, "r");
                //计算文件的长度
                 long length=raf.length();
                 //每一份文件的大小
                 //lenght/count;
                 //long 类型除以int 类型 是long类型
                 long maxSize = length/count;
                 //定义切割文件的偏移量
                 long offSet = 0L;     
                 for(int i=0;i<count-1; i++){//-1 是最后一份先不处理
                     //初始化标记
                     long begin = offSet;
                     //切割到第几份文件
                     long fend = (i+1)*maxSize; 
                     //切割写入磁盘 file 放入的文件地址 i 切割一份就放入一份  Offset 到哪里切割 
                     offSet = getWrite(file,i,offSet,fend);
                 }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(raf!=null)
                    try {
                        raf.close();
                    } catch (IOException e) {
                    }
                
            }

    }
   /**
    * @param:getWrite
    * @param file 切割文件
    * @param index 标记
    * @param begin 开始位置
    * @param end 结束位置
    * @return
    */
//创建文件切割的方法  参数 string file  int index第几份文件  long begin longend 
public static long getWrite(String file,int index,long begin,long end){
        long endPointer = 0L;
        int n = 0;
        RandomAccessFile in = null;
        RandomAccessFile out = null;
    try {
         in = new RandomAccessFile(file,"r");
         out = new RandomAccessFile(file+"-"+index+".tmp","rw");
        //存储在数组里
        //byte b[] = new byte[1024];要规范虽然不会报错
          byte[] b=new byte[1024];
          
          //从指定位置开始读取
          in.seek(begin);
          while((n=in.read(b))!=-1 && in.getFilePointer()<=end){
              out.write(b,0,n);
          }
          //返回文件记录指针的当前位置
          endPointer = in.getFilePointer();
        
    } catch (FileNotFoundException e) {
        
    }catch (IOException e) {
        
    }finally{                
            try {
                if(out!=null)out.close();
                if(in!=null)in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                                    
    }
    //写入文件
                return endPointer;
            }
        

}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值