把大文件分成多个小文件(内含hash值比较文件是否相同方法)

/**
* 分割文件

* @param filePath
*            要分割的文件路径
* @param num
*            要分割成多少份
* @author shandowF
* @date 2016年3月2日
*/
public static void cut(String filePath, int num) {
File file = new File(filePath);
long lon = file.length() / num;
// +1L
try {
//
RandomAccessFile raf1 = new RandomAccessFile(file, "r");
byte[] bytes = new byte[1024];// 值设置越小,则各个文件的字节数越接近平均值,但效率会降低,这里折中,取1024
int len = -1;
for (int i = 0; i < num; i++) {
String name = getFileBegin(filePath) + getYYMMDD() + i + getFileSuff(filePath);
System.out.println(name);
File file2 = new File(name);
RandomAccessFile raf2 = new RandomAccessFile(file2, "rw");
while ((len = raf1.read(bytes)) != -1) {// 读到文件末尾时,len返回-1,结束循环
raf2.write(bytes, 0, len);
if (raf2.length() > lon)// 当生成的新文件字节数大于lon时,结束循环
break;
}
raf2.close();
}
raf1.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


/**
* 恢复文件

* @author shandowF
* @date 2016年3月5日
*/
public static void merge(String nowFilePath, String oldFilePath, int num) {
File file = new File(nowFilePath);
try {
// 写出文件
RandomAccessFile target = new RandomAccessFile(file, "rw");
for (int i = 0; i < num; i++) {
String oldFile = getFileBegin(oldFilePath) + getYYMMDD() + i + getFileSuff(oldFilePath);
File file2 = new File(oldFile);
RandomAccessFile src = new RandomAccessFile(file2, "r");
byte[] bytes = new byte[1024];// 每次读取字节数
int len = -1;
while ((len = src.read(bytes)) != -1) {
target.write(bytes, 0, len);// 循环赋值
}
src.close();
}
target.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}



/**
* 清洗文件让文件中没有重复的值

* @author shandowF
* @date 2016年3月5日
*/
public static void clear(File file, String newPath) {
try {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader buffer = new BufferedReader(new InputStreamReader(fis, "utf-8"), 20 * 1024 * 1024);// 用5M的缓冲读取文本文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new File(newPath)), "utf-8");
Set<String> set = new HashSet<String>();
String temp = ""; // 临时字符串
int x = 0;
while ((temp = buffer.readLine()) != null) { // 读文件,一行读一个
set.add(temp); // 存储到Set集合里面
if (x % 30000 == 0) {
// System.out.print("..");
}
x++;
}
fis.close();
buffer.close(); // 关闭读取操作


for (String xxser : set) {
out.write(xxser + "\r\n");


}
out.close(); // 关闭写操作
} catch (Exception e) {
e.printStackTrace();
}

}

/**

* 获取文件除后缀名外的名字
* @author shandowF
* @date 2016年3月5日
*/
public static String getFileBegin(String filePath) {
String i = getFileSuff(filePath);
int index = filePath.indexOf(i);
return filePath.substring(0, index);
}


/**
* 获取文件后缀名

* @author shandowF
* @date 2016年3月5日
*/
public static String getFileSuff(String filePath) {
int start = filePath.indexOf(".");
int end = filePath.length();
return filePath.substring(start, end);

};




/**
* 获取当天 yy-mm-dd

* @return
*/
public static String getYYMMDD() {
return mmdd.format(new Date());
}





/**
* 判断两个文件是否相等

* @author shandowF
* @date 2016年3月5日
*/
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[8192];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer)) != -1) {
digest.update(buffer, 0, len);
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值