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;

        }
   }}
                           

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM