您的位置:首页 > 其它

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

2018-03-05 15:00 337 查看
/**
* 分割文件

* @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 sta
a41d
tic 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();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IO 文件处理