zoukankan      html  css  js  c++  java
  • 利用IO流把单个文件切割成碎片文件

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class SplitFileTest {
    
        public static void main(String[] args) throws IOException {
            
            /*
             * 文件切割器
             * 一个读取流,对应多个输出流。而且生成的碎片文件都有有序的编号
             */
            File srcFile = new File("d:\Test.java");
            File destDir = new File("tempfile\partfiles");
            fileSplit(srcFile,destDir);
            
        }
    
        public static void fileSplit(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[1024*20];//1M
            int count = 0;
            int len = 0;
            //4.往缓冲区中存储数据
            while((len=fis.read(buf))!=-1){
    //            System.out.println(len);
                //5.创建输出流,并明确要写入的文件对象
                File partFile = new File(destDir,(++count)+".part");
                fos = new FileOutputStream(partFile);
                fos.write(buf,0,len);
                
                
            }
            //应该在产生碎片文件时,需要产生一个配置文件,至少记录碎片的个数和源文件的名字
            //partcount = 5; filename = Test.java
            
            //配置文件中存储的是键值信息,使用properties集合
            Properties prop = new Properties();
            prop.setProperty("partcount", Integer.toString(count));
            prop.setProperty("filename", srcFile.getName());
            
            File configFile = new File(destDir,(++count)+".properties");
            fos = new FileOutputStream(configFile);
            prop.store(fos, "save part file info");
            
            fis.close();
            
        }
    
    }
  • 相关阅读:
    初级工程师该如何去学习,如何去研发开关电源?
    CentOS-pam认证机制简介
    Linux-密码复杂度限制
    如何写一个简单的webserver(一):最简实现
    MySQL5.6绿色版安装(mysql-5.6.24-winx64.zip)
    Linux忘记roo密码的解决办法
    Linux中的netstat命令详解
    WireShark抓包分析(二)
    Wireshark抓取TCP包分析
    Wireshark 基本介绍和学习 TCP 三次握手
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5926104.html
Copyright © 2011-2022 走看看