IO流之 读 操作

    /*

      可以放在main方法中运行,也可以放进线程,最好是放进线程。

      与前一篇写操作可以直接拿去运行看效果

    */

    BufferedReader br = null;   //读的流操作 
        try {
            br = new BufferedReader(new InputStreamReader(sk.getInputStream()));   //三根通道的对接
            String str = br.readLine();    //用一个字符串来接收
            String [] str1 = str.split("&");   //用拆分将接收的消息才分成几段放进数组中
            Date sendTime = new Date(Long.parseLong(str1[2]));  //获取当前时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //设置时间格式
            System.out.println(sdf.format(sendTime));   //显示时间
            System.out.println(str1[0]+"说:"+"\n"+str1[1]);   //显示写操作端发出的消息
            
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();    //关闭流
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
            }
        }

posted @ 2016-06-26 22:32  Blueses  阅读(190)  评论(0编辑  收藏  举报