java 文件分割 合并 代码

import java.io.*;

class Fen {
String fileName;

int size;

Fen(String fileName, String size) {
this.fileName = fileName;
this.size = Integer.parseInt(size) * 1024;
}

public void cut() throws Exception {
int maxx = 0;
File inFile = new File(fileName);

int fileLength = (int) inFile.length(); // 取得文件的大小
int value; // 取得要分割的个数

RandomAccessFile inn = new RandomAccessFile(inFile, "r");// 打开要分割的文件

value = fileLength / size;

int i = 0;
int j = 0;

// 根据要分割的数目输出文件
for (; j < value; j++) {
File outFile = new File(inFile.getName() + j + "zzii");
RandomAccessFile outt = new RandomAccessFile(outFile, "rw");
maxx += size;
for (; i < maxx; i++) {
outt.write(inn.read());
}
outt.close();
}
File outFile = new File(inFile.getName() + j + "zzii");
RandomAccessFile outt = new RandomAccessFile(outFile, "rw");
for (; i < fileLength; i++) {

outt.write(inn.read());
}
outt.close();

inn.close();
}
}

class He {
String fileName;

String filterName;

He(String fileName, String filterName) {
this.fileName = fileName;
this.filterName = filterName;
}

public void unite() throws Exception {
String[] tt;
File inFile = new File("."); // 在当前目录下的文件
File outFile = new File(fileName); // 取得输出名
RandomAccessFile outt = new RandomAccessFile(outFile, "rw");

// 取得符合条件的文件名
tt = inFile.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
String rr = new File(name).toString();
return rr.endsWith(filterName);
}
});
// 打印出取得的文件名
for (int i = 0; i < tt.length; i++) {
System.out.println(tt[i]);
}

// 打开所有的文件再写入到一个文件里
for (int i = 0; i < tt.length; i++) {
inFile = new File(tt[i]);
RandomAccessFile inn = new RandomAccessFile(inFile, "r");
int c;
while ((c = inn.read()) != -1)
outt.write(c);
}

outt.close();
}
}

public class test {
public static void main(final String[] args) throws Exception {

if (args.length == 0) {
print();
return;
}
if (args[0].equals("-c")) {
Fen cutt = new Fen("f:/1.rar", "1024");
cutt.cut();
} else if (args[0].equals("-r")) {
He hee = new He("d:/1.rar", "zzii");
hee.unite();
} else
print();

}

public static void print() {
System.out
.println("usage:n分: java test -c file1 (单位为K)n合 java test -r file2(我设置的方便标识)");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值