zoukankan      html  css  js  c++  java
  • java==IO练习,文件切割合拼工具开发代码

    package 递归;
    /*
     * 1、递归一定要明确条件,否则容易栈内存溢出
     * 2、注意递归次数,次数太多容易造成栈内存溢出
     * */
    public class DiGuiDemo {
    
        public static void main(String[] args) {
            //toBin(6);
            int sum = getSum(5);
            System.out.println(sum);
    
        }
        public static void toBin(int num){
            if(num>0){
                System.out.println(num%2);
                toBin(num/2);
            }
        }
        public static int getSum(int num){
            if(num==1)
                return 1;
            return num+getSum(num-1);
        }
    
    }
    package 文件切割合拼;
    
    import java.io.File;
    import java.io.FilenameFilter;
    
    public class filByName implements FilenameFilter {
        private String x;
        public filByName(String x){
            super();
            this.x=x;
        }
    
        @Override
        public boolean accept(File dir, String name) {
    
            return name.endsWith(x);
        }
    
    }
    package 文件切割合拼;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class SplitFileDemo {
        public static final int SIZE = 1024*1024;
    
        public static void main(String[] args) throws IOException {
            File ff1 = new File("d:\linux.doc");
    
            splitFile_2(ff1);
            
        }
    
        public static void splitFile_2(File ff1) throws IOException {
            FileInputStream fis = new FileInputStream(ff1);
            byte[]buf = new byte[SIZE];
            int len =0 ;
            int count =0;
            File dir = new File("d:\filepart");
            if(!dir.exists()){
                dir.mkdirs();
            }
            FileOutputStream fos = null;
            while((len=fis.read(buf))!=-1){
                fos= new FileOutputStream(new File(dir,(count++)+".part"));
                fos.write(buf,0,len);
                fos.close();
            }
            Properties p = new Properties();
            FileOutputStream fos1 = new FileOutputStream(new File(dir,".Properties"));
            p.setProperty("filepart", count+"");
            p.setProperty("filename", ff1.getName());
            p.store(fos1, "file part info");
            fis.close();
            fos1.close();
        }
    
        public static void splitFile(File file) throws IOException {
            FileInputStream f1 = new FileInputStream(file); 
            byte[]buf = new byte[SIZE];
            FileOutputStream fo1 = null;
            int len = 0;
            int count=1;
            File dir = new File("d:\partfiles");
            if(!dir.exists()){
                dir.mkdirs();
            }
            while((len=f1.read(buf))!=-1){
                fo1 = new FileOutputStream(new File(dir,(count++)+".part"));
                fo1.write(buf,0,len);
            }
            f1.close();
            fo1.close();
        }
    
    }
    package 文件切割合拼;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Enumeration;
    import java.util.Properties;
    
    /**
     * 文件合拼:
     * 思路:
     * 1、过滤.properties读取配置信息,获取filepart,filename
     * 2、过滤.part,序列流读取.part文件
     * 3、写入指定磁盘文件夹中
     * */
    public class MegerFileDemo {
    
        public static void main(String[] args) throws IOException {
            File dir = new File("d:\filepart");
            megerFile(dir);
    
        }
        public static void megerFile(File dir)throws IOException{
            
            File[]names = dir.listFiles(new filByName(".Properties"));
            File confile=names[0];
            if(names.length!=1){
                throw new RuntimeException("文件加载出错,请重新下载此文件再试");
            }
            Properties p = new Properties();
            FileInputStream fis = new FileInputStream(confile);
            p.load(fis);
            int count = Integer.parseInt(p.getProperty("filepart"));
            String name= p.getProperty("filename");
            File[]parts = dir.listFiles(new filByName(".part"));
            if(parts.length!=count){
                throw new RuntimeException("文件个数不对,请重新下载");
            }
            ArrayList<FileInputStream> arr1 = new ArrayList<FileInputStream>();
            for(int x=0;x<parts.length;x++){
                //file对象就含有文件路径,所以下面可以使用parts[x]代替new File(dir,(x+".part"))
                arr1.add( new FileInputStream(new File(dir,(x+".part"))));
            }
            Enumeration<FileInputStream> en =  Collections.enumeration(arr1);
            SequenceInputStream sis = new SequenceInputStream(en);
            FileOutputStream fos = new FileOutputStream(new File(dir,name));
            byte[]buf = new byte[1024*1024];
            int len= 0;
            while((len=sis.read(buf))!=-1){
                fos.write(buf, 0, len);
            }
            sis.close();
            fos.close();
        }
    
    }
  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6859315.html
Copyright © 2011-2022 走看看