有500万条记录放在C盘上,现在需要将这500万条记录平均拆分到n个磁盘上,如何实现才能效率最优?
例如:n=2的时候,有两个盘disk1,disk2,分别是D://和E://,这时候奇数行的记录都在D盘,偶数行的记录都在E盘;
当n==5的时候,编号为5k+1的行放在disk1,5k+2的行号的行放在disk2上。。。以此类推。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
参考这个
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.Random;
public class Test {
public static void main(String[] args) throws Exception {
File src = new File("D:\\src.txt");
File dest = new File("D:\\dest");
FileReader fr = new FileReader(src);
BufferedReader br = new BufferedReader(fr);
int lineno = 1;
int index = 1;
dest.mkdirs();
FileWriter fw = new FileWriter(new File(dest, creIndex(index++)
".txt"));
(br.ready()) {
(lineno++ % 500 == 0) {
fw.close();
= new FileWriter(new File(dest, creIndex(index++) + ".txt"));
}
String line = br.readLine();
fw.write(line + "\r\n");
}
fw.close();
br.close();
fr.close();
}
public static String creIndex(int num) {
DecimalFormat df = new DecimalFormat("00");
return df.format(num);
}
}
记得用多线程