日期:2014-05-20 浏览次数:20650 次
import java.io.*; import java.util.*; public class ReadFile { public static void main(String[] args) throws Exception { TreeSet<Integer> set = new TreeSet<Integer>(); //首先生成20000个随机数到set中 int num = 0; while (true) { if (set.add((int) (Math.random() * 40000000))) { num++; } if (num == 20000) { break; } } //根据set的数据读取响应的行数据 BufferedReader in = new BufferedReader(new FileReader("in.txt")); PrintWriter out = new PrintWriter(new FileWriter("out.txt")); for (int i = 0; i < 40000000; i++) { String str = in.readLine(); if (set.contains(i)) { out.println(str); } } in.close(); out.close(); } }