javaSE中的输入输出流---一个读取流,对应多个输出流。而且生成的碎片文件都有有序的编号

<span style="font-size:18px;">package com.io.other.split;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


public class SplitFileTest {
/**
 * 文件切割器。
 * 一个读取流,对应多个输出流。而且生成的碎片文件都有有序的编号
 * @param args
 * @throws IOException 
 */
	private static final int DEFAULT_SIZE=1024*2;
	public static void main(String[] args) throws IOException {
		File srcFile=new File("chen\\11.avi");
		File destDir=new File("chen\\partfiles");
		SplitFile(srcFile,destDir);
	}
	/**
	 * 
	 * @param srcFile
	 * @throws IOException 
	 */

	public static void SplitFile(File srcFile,File destDir) throws IOException {
		
		if (!srcFile.exists()) {
			throw new RuntimeException(srcFile.getAbsolutePath()+",源文件不存在");
		}
		
        if(!destDir.exists()){
			destDir.mkdirs();
		}
		//1、定义一个源文件
		FileInputStream fis=new FileInputStream(srcFile);
		//2、创建目的引用
		FileOutputStream fos=null;
		//3、创建一个缓冲区存储数据
		byte[] buf=new byte[DEFAULT_SIZE];
		int len=0;
		int count=0;
		while((len=fis.read(buf))!=-1){
			//创建输出流,并明确要写入的文件
			File partFile=new File(destDir,(++count)+".part");
			fos=new FileOutputStream(partFile);
			fos.write(buf, 0, len);
			fos.close();
		}
		//在产生碎片文件时,需要产生一个配置文件,记录碎片的个数和源文件名字
		//partfile=n;filename=11.avi
		
		//配置文件中存储的键值信息。使用Properties集合
		Properties prop=new Properties();
		
		prop.setProperty("partcount", Integer.toString(count));
		prop.setProperty("filename", srcFile.getName());
		File conFile=new File(destDir,(++count)+".properties");
		fos=new FileOutputStream(conFile);
		//关闭资源
		fos.close();
		fis.close();
	}

}
</span>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值