큰 파일 을 작은 파일 로 분해 자바

2604 단어 자바 기반


package zx.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileSpiliter {

/**
*
* @param rows
* @param sourceFilePath
* @param targetDirectoryPath
*/
public static void splitDataToSaveFile(int rows, String sourceFilePath,
String targetDirectoryPath) {
long start1 = System.currentTimeMillis();

File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetDirectoryPath);
if (!sourceFile.exists() || rows <= 0 || sourceFile.isDirectory()) {
return;
}
if (targetFile.exists()) {
if (!targetFile.isDirectory()) {
return;
}
} else {
targetFile.mkdirs();
}
try {

InputStreamReader in = new InputStreamReader(new FileInputStream(sourceFilePath),"UTF-8");
BufferedReader br=new BufferedReader(in);

BufferedWriter bw = null;
StringBuilder str = new StringBuilder();
String tempData = br.readLine();
int i = 1, s = 0;
long start2 = System.currentTimeMillis();
while (tempData != null) {
str.append(tempData + "\r
");
if (i % rows == 0) {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
targetFile.getAbsolutePath() + "/" + sourceFile.getName() +"_" + (s+1) +".log"), "UTF-8"),1024);

bw.write(str.toString());
bw.close();

str = new StringBuilder();
start2 = System.currentTimeMillis();
s += 1;
}
i++;
tempData = br.readLine();
}
if ((i - 1) % rows != 0) {

bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
targetFile.getAbsolutePath() + "/" + sourceFile.getName() +"_" + (s+1) +".log"), "UTF-8"),1024);
bw.write(str.toString());
bw.close();
br.close();

s += 1;
}
in.close();

} catch (Exception e) {
}
}

public static void main(String[] args) {
FileSpiliter.splitDataToSaveFile(100000, "D:/log/out.log-20180927_10.33.100.168", "D:/log/out.log-20180927/");
}


}


좋은 웹페이지 즐겨찾기