当前位置 : 主页 > 编程语言 > c++ >

读取文本批量存储一次读出

来源:互联网 收集:自由互联 发布时间:2021-06-30
读取文件批量存储一次读出 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 + "次处理结束=====");
			}
		}
网友评论