鱼尾九八

好好努力

键盘录入与控制台输出并写入文件

package yuwei.com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * 需求
 * 源:键盘录入
 * 目的:控制台输出
 * */
public class OutputStreamWriterDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //InputStream in = System.in;
        //InputStreamReader is = new InputStreamReader(in);
        //装饰
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


        //OutputStream out = System.out;
        //OutputStreamWriter osw = new OutputStreamWriter(out);
        BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));

        BufferedWriter bufw1 =null;
        String line = null;
        try {
            /*
             * 新需求:键盘录入,写入文件
             * */
            //bufw1 = new BufferedWriter(new FileWriter("out.txt"));
            //指定格式
            bufw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("out.txt"),"utf-8"));


            while((line = br.readLine())!=null) {
                if("over".equals(line)) {
                    break;
                }else {
                    //会在控制台输出
                    bufw.write(line.toUpperCase());
                    bufw.newLine();
                    bufw.flush();

                    //会写进文件
                    bufw1.write(line.toUpperCase());
                    bufw1.newLine();
                    bufw1.flush();
                }
            }
        }catch(IOException e) {
            System.out.println(e.toString());
        }finally {
            try {
                br.close();
                bufw.close();
                if(bufw1 !=null) {
                    bufw1.close();
                }

            }catch(IOException e) {
                System.out.println(e.toString());
            }
        }   

    }

}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40051278/article/details/80690314
个人分类: Java IO
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭