主要是测试技术上是否可行,没有做任何结构上的设计,代码可能有不严密或者错误的地方,只供参考,不能直接应用在生产环境。
思路:
记住每一行开始的位置(相对文件开始的偏移byte数),用随机访问类RandomAccessFile打开文件
根据行号,得到对应的位置。定位到该位置,读整行内容。下面是代码,在eclipse调试通过。
public class RFile {
public static void main(String[] args) throws Exception {
Map map = new HashMap();
PrintWriter writer = new PrintWriter("1.txt", "UTF-8");
Random random = new Random();
long pos = 0;
int linecount = 5000000;
int maxFieldCount = 5;
for (int i = 0; i < linecount; i++) {
if (i % 30000 == 0)
System.out.println(i);
StringBuffer line = new StringBuffer();
line.append("_id:");
line.append(i + 1);
line.append("|");
int r = random.nextIn