java 文件的拆分与合并

拆分

package com.work;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class Test1 {
	
	public static void main(String[] args) {
		File f =  new File("/home/roo/桌面/chai/面向对象.pdf");
		split(f);
	}
	public static void split(File f) {
		byte[] data = new byte[(int) f.length()];
		try {
			FileInputStream fis = new FileInputStream(f);
			fis.read(data);// 读取文件到data中
			fis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(data.length / 1024 + "k");
		int len = 1024 * 100;
		// 拆分为100k的文件夹
		int n = (int) (f.length() / len);
		// 剩下的不足100k的放另一个
		int m = (int) (f.length() / len);
		// 文件数组,用来存放文件。长度为总共需要生成文件的个数。
		File[] fs = new File[n + 1];
		for (int i = 0; i < n + 1; i++) {
			int start = i * 102400;
			int end = (i + 1) * 102400;
			fs[i] = new File(f + "-" + i);
			if (i < n) {
				byte[] b = Arrays.copyOfRange(data, start, end);
				try {
					FileOutputStream fos = new FileOutputStream(fs[i]);
					fos.write(b);
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				start = i * 102400;
				end = (int) f.length();
				byte[] b = Arrays.copyOfRange(data, start, end);
				try {
					FileOutputStream fos = new FileOutputStream(fs[i]);
					fos.write(b);
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			System.out.println("子文件夹:" + fs[i].getAbsolutePath() 
				+ "  大小: " + fs[i].length());
		}
	}
}

合并

package com.work;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Test2 {
	public static void main(String[] args) throws IOException {
		File f1 = new File("/home/roo/桌面/z1/o");
		File [] ff= f1.listFiles();
		File f2 = new File("/home/roo/桌面/z2/p");
		com(ff,f2);
	}
public static void com(File[] fs,File newFile) {
		int len = 0;
		for (File file : fs) {
			len += file.length(); 
		}
		//数组放合并的内容
		byte[] b = new byte[len];  
		int len1 = 0;
		for (File file : fs) {
			byte[] bi = new byte[(int) file.length()];
			try {
				FileInputStream fis = new FileInputStream(file);
				fis.read(bi);
				fis.close();
				System.out.println(bi.length);
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.arraycopy(bi, 0, b, len1, (int) file.length());
			len1 +=file.length();
			try {
				FileOutputStream fos = new FileOutputStream(newFile);
				fos.write(b);
				fos.close();
			}  catch (IOException e) {
				e.printStackTrace();
			}
		}	
		
}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值