zoukankan      html  css  js  c++  java
  • 控制台输入聊天记录 输出到文件中保存 Day20

    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();
                    }
                }
            }
            
        }
    }
  • 相关阅读:
    四色定理+dfs(poj 1129)
    栈的应用:表达式求值运算
    多重背包 (poj 1014)
    poj 1080 (LCS变形)
    KMP算法(快速模式匹配)
    贪心+构造( Codeforces Round #344 (Div. 2))
    JavaScript Ajax
    Canvas绘图
    TCP/IP协议
    移动端click事件延迟300ms到底是怎么回事,该如何解决?
  • 原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/6770507.html
Copyright © 2011-2022 走看看