• 利用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();
            
        }
    
    }
  • 相关阅读:
    [POJ2456]Aggressive cows(贪心,二分查找)
    [POJ1064]Cable master
    [Hadoop]单机尝试安装并测试Hadoop2.7.1(附带注释脚本)
    [HDOJ5500]Reorder the Books
    [UVA11076]Add Again
    [BNU弱校联萌]背水一战
    [HDOJ4911]Inversion
    POJ2735/Gym 100650E Reliable Nets dfs
    Gym 100650H Two Ends DFS+记忆化搜索
    HDU 4292 Food 最大流
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5926104.html
Copyright © 2020-2023  润新知