文件分割合并类(java)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;



// 文件分割类
public class FILECut {
	private OutputStream ou;
	private File file;
	private long fileLength;
	private byte[] buf;
	private RandomAccessFile saveFile;
	public FILECut(String f)
	{
		file = new File(f);
		if (file.exists())
		{
			fileLength = file.length();
		}
		else
		{
			fileLength = 0;
		}
	}


	public void Cut(int n, String fn, String tgt) throws IOException
	{
		File fe;
		int seeklen = 0;
		int tpLen = 0;
		int tpDe  = 0;
		if (fileLength%n==0)
		{
			tpLen = (int)(fileLength/n);
		}
		else
		{
			tpLen = (int)(fileLength/n);
			tpDe  = (int)(fileLength%n);
		}
		
		saveFile = new RandomAccessFile(file, "r");
		saveFile.seek(0);
			
		for (int i=1; i<=n; i++)
		{
			fe = new File(file.getParent()+"\\"+fn+"_"+i+"."+tgt);
			if (i<n)
			{
				if (tpLen!=0)
				{
					buf = new byte[tpLen];
					saveFile.read(buf);
					ou = new FileOutputStream(fe);
					ou.write(buf);
					seeklen+=tpLen;
					saveFile.seek(seeklen);
					buf = null;
				}
			}
			else if(i==n)
			{
				if (tpDe!=0)
				{
					buf = new byte[tpLen+tpDe];
					saveFile.read(buf);
					ou = new FileOutputStream(fe);
					ou.write(buf);
					buf = null;
				}
			}
		}
		saveFile.close();
	}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

//文件合并类
public class FILEHut {
	private static File[] fileList;
	private static RandomAccessFile saveFile;
	private static InputStream in;
	private static byte[] buf;
	
	private static Boolean isCutFile(File f, String tg)
	{
		String file = f.getName();
		String hz = file.substring(file.lastIndexOf(".")+1).toLowerCase();
		if (hz.compareTo(tg)==0)
		{
			return true;
		}
		return false;
	}
	public static void Hut(String fname, String tgt) throws IOException
	{
		File f = new File(new File(fname).getParent());
		fileList = f.listFiles();
		int seeklen = 0;
			saveFile = new RandomAccessFile(fname, "rw");
			saveFile.seek(0);

			for (int i=0; i<fileList.length; i++)
			{
				if (isCutFile(fileList[i], tgt)==true)
				{
					in = new FileInputStream(fileList[i]);
					buf = new byte[(int) fileList[i].length()];
					in.read(buf);
					saveFile.write(buf);
					seeklen+=(int) fileList[i].length();
					saveFile.seek(seeklen);
				}
			}
			saveFile.close();
			
			
	}
}

使用详解:

		FILECut file = new FILECut("D:\\cut\\cut.txt"); //分割文件路径
		file.Cut(3, "btt","tt"); //分割份数 前缀文件名 后缀文件名(格式.tt)
		
		FILEHut.Hut("D:\\cut\\cd.exe", "tt"); //将制定目录下得所有格式为tt的文件合成cd.exe 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值