读取文件批量存储一次读出 public static void main(String args[]) throws Exception { File file = new File("src/test.txt"); System.out.println(file.getAbsolutePath());FileOutputStream fos = new FileOutputStream(file);for(int i = 0;
public static void main(String args[]) throws Exception { File file = new File("src/test.txt"); System.out.println(file.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(file); for(int i = 0;i<10000;i++) { fos.write((i + "\r\n").getBytes()); //往文件写入数据 } fos.flush(); //刷出数据 FileReader fr = new FileReader(file); //读取文件 BufferedReader br = new BufferedReader(fr); int readLineNum = 0; int processCount = 1; String line = ""; StringBuffer sb = new StringBuffer(); while((line=br.readLine())!=null) { readLineNum++; sb.append(line + "\r\n"); //存储1000条记录 if (readLineNum % 1000 == 0) { System.out.println("====第" + processCount + "次处理====="); //当1千条记录到了打印默认为一 System.out.println(sb); //输出 sb.delete(0, sb.length()); System.out.println("=====第" + processCount + "次处理结束====="); processCount++; //处理次数加1 } }if(readLineNum%1000>0) //余下的一次性处理 { System.out.println("====第" + processCount + "次处理====="); System.out.println(sb); sb.delete(0, sb.length()); System.out.println("=====第=========" + processCount + "次处理结束====="); } }