zoukankan      html  css  js  c++  java
  • java文件分割及合并

    分割设置好分割数量,根据源文件大小来把数据散到子文件中代码如下;

    package word;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    
    
    public class Bb {
        
       
        public static void main(String[] args) throws FileNotFoundException {
            // TODO 自动生成的方法存根
            
            String path1="C:";
            String name="\JAVA";
            String end=".txt";
            String path2;
            
            long l;
            long n=2;//设置分割数目
            File file = new File(path1+name+end);
            File file2; 
            l=file.length();
            InputStream in = null;
            PrintStream out = null;
            try {
             
                in = new FileInputStream(file);
                int tempbyte;
                int i=0;
                for(i=1;i<=n;i++) {
                    path2=path1+name+i+end;
                    file2=new File(path2);
                    if(file2.exists()) {}
                    else {
                        file2.createNewFile();
                    }
                    out=new PrintStream(file2);
                while ((tempbyte =  in.read()) != -1) {
                    
                out.print((char)tempbyte);
                System.out.write(tempbyte);
                if(file2.length()>l/n)break;//子文件到达指定大小就创建下一个文件;
                }
                
                
                }
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
    
            }
       }}
       

    合并就是上面的反过程,即把各个小文件输入到大文件中:

    package word;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;
    
    
    
    public class Bb {
        
       
       
        public static void main(String[] args) throws FileNotFoundException {
            // TODO 自动生成的方法存根
            
            String path1="C:";
            String name="\JAVA";
            String end=".txt";
            String path2;
            
            
            long n=2;
            File file = new File(path1+name+end);
            File file2; 
            l=file.length();
            InputStream in = null;
            PrintStream out = null;
                    if(file.exists()) {}
            else {
            file.createNewFile();
                    }
                    out=new PrintStream(file);
                try {
                
                
                
                int tempbyte;
                int i=0;
                for(i=1;i<=n;i++) {
                    path2=path1+name+i+end;
                                    file2=new File(path2);
                                    in = new FileInputStream(file2);
                    
                    
                    
                while ((tempbyte =  in.read()) != -1) {
                    
                out.print((char)tempbyte);
            
                }
                
                
                }
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
    
            }
       }}
                               
  • 相关阅读:
    左耳听风笔记之一
    富爸爸穷爸爸 -- 笔记
    Aruba无线控制器常用操作
    接入交换机办公网常用配置
    核心交换机办公网常用配置
    FortiGate防火墙办公网常用配置
    去掉深信服上网认证页面里的“修改密码”
    深信服上网行为管理短信认证多用户登录问题
    深信服上网行为管理配置跨三层MAC识别
    深信服上网行为管理实现一次认证成功之后连续3天无流量通过才再次认证
  • 原文地址:https://www.cnblogs.com/liuleliu/p/11830983.html
Copyright © 2011-2022 走看看