public class Test {
public static void main(String[] args) {
String srcPath = "c:\\test\\main.txt";
splitFile(srcPath);
}
public static void splitFile(String srcPath) {
BufferedReader bufferedReader = null;
try {
File file = new File(srcPath);
bufferedReader = new BufferedReader(new FileReader(file));
String line = null;
int lineNum = 0;
String symbolContent = null;
while ((line = bufferedReader.readLine()) != null) {
lineNum++;
if (lineNum == 1) {
symbolContent = line;
}
}
System.out.println("第一行数据:" + symbolContent);
int total = Integer.valueOf(symbolContent.substring(11));
System.out.println("记录总数:" + total);
int threadNum = 10;
//计算每个线程处理记录数
int singleProcess = 0;
if (total % threadNum == 0) {
singleProcess = total / threadNum;
} else {
singleProcess = (total / threadNum) + (total % threadNum);
}
System.out.println("每个线程处理记录数:" + singleProcess);
CountDownLatch latch = new CountDownLatch(threadNum);
long start = System.currentTimeMillis();
for (int i = 0; i < threadNum; i++) {
int startIndex = i * singleProcess;
MultiThreadReadFile multiThreadReadFile = new MultiThreadReadFile(file, startIndex, singleProcess, latch);
Thread thread = new Thread(multiThreadReadFile);
thread.start();
}
latch.await();
long end = System.currentTimeMillis();
System.out.println("拆分文件耗时:" + (end - start) + "毫秒");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class MultiThreadReadFile implements Runnable {
private File file;
private int startIndex;
private int size;
private CountDownLatch latch;
public MultiThreadReadFile(File file, int startIndex, int size, CountDownLatch latch) {
this.file = file;
this.startIndex = startIndex;
this.size = size;
this.latch = latch;
}
@Override
public void run() {
readFile(file, startIndex, size);
}
public void readFile(File file, int startIndex, int size) {
String outFilePath = "c:\\test\\dir.txt";
File outFile = new File(outFilePath);
LineNumberReader reader = null;
BufferedWriter writer = null;
try {
reader = new LineNumberReader(new FileReader(file));
writer = new BufferedWriter(new FileWriter(outFile));
int endIndex = startIndex + size;
String line = null;
int currentNum = 0;
while ((line = reader.readLine()) != null && (currentNum = reader.getLineNumber()) < (endIndex + 2)) {
if (currentNum != 1 && currentNum >= startIndex + 2) {
writer.write(line + "\r");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
latch.countDown();
}
}
}