package com.sxt.io; /* 字节流转换成字符流(转换流) * InputStreamReader extends Reader * OutputStreamWriter extends Writer */ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.util.Date; public class TestParse { public static void main(String[] args){ BufferedReader br = null; BufferedWriter bw = null; BufferedWriter bw2 = null; try { br = new BufferedReader(new InputStreamReader(System.in)); bw = new BufferedWriter(new OutputStreamWriter(System.out)); bw2 = new BufferedWriter(new FileWriter("G:\testCopy.txt")); int count = 0; String line = ""; while (true) { if(count % 5 == 0){ String date = new Date().toLocaleString(); bw.write(date); bw.newLine(); bw.flush(); bw2.write(date); bw2.newLine(); bw2.flush(); } if(count%2 == 0){ String string = "小明说:"; bw.write(string); bw.flush(); line = br.readLine();// bw.newLine(); bw2.write(string); bw2.flush(); bw2.write(line); bw2.flush(); bw2.newLine(); }else if(count%2 == 1){ String string = "小刚说:"; bw.write(string); bw.flush(); line = br.readLine();// bw.newLine(); bw2.write(string); bw2.flush(); bw2.write(line); bw2.flush(); bw2.newLine(); } count++; if(line.equalsIgnoreCase("over")){ bw2.write("一共"+count+"次聊天记录!"); bw2.flush(); break; } } }catch (IOException e) { e.printStackTrace(); }finally{ if(br != null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(bw != null){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } if(bw2 != null){ try { bw2.close(); } catch (IOException e) { e.printStackTrace(); } } } } }