IO_标准的输入输出流、打印流、数据流

标准的输出流System.out、System.in

/**
 * @date 2018/7/8 22:06
 * 标准的输入输出流
 * 标准的输出流 System.out(); 返回一个InputStream
 * 标准的输入流 System.in(); 返回一个PrintStream
 */
public class StandardInputOrOutput {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            InputStream is = System.in;
            InputStreamReader isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            String str = null;
            while (true){
                System.out.println("请输入字符流");
                str = br.readLine();
                if (str.equalsIgnoreCase("e")){
                    break;
                }
                System.out.println(str.toUpperCase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

打印流

/**
 * @date 2018/7/8 23:03
 * 打印流:字节流:PrintStream 字符流:PrintWriter
 */
public class Print {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("F:/test/1.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //创建打印输出流,设置为自动刷新模式(写入换行符或字节“\n”时都会刷新输出缓冲区)
        PrintStream ps = new PrintStream(fos);
        if (ps!=null){
            //把标准输出流(控制台输出)改成文件
            System.setOut(ps);
        }
        //输出ASCII字符、
        for (int i = 0; i < 255; i++) {
            System.out.print((char)i);
            if (i%50==0){
                System.out.println();//换行
            }
        }
        ps.close();

    }
}
数据流

/**
 * @author chenpeng
 * @date 2018/7/8 23:21
 * 数据流:用来处理基本数据类型、String、字节数组的数据:DataInputStream、DataOutputStream
 */
public class DataStream {
    public static void main(String[] args) {
        DataOutputStream dos = null;
        try {
            //创建连接到指定文件的数据输出流对象
            FileOutputStream fos = new FileOutputStream("F:/test/1.txt");
            dos = new DataOutputStream(fos);
            //写UTF字符串
            dos.writeUTF("I love you");
            //写入布尔值
            dos.writeBoolean(true);
            //写入长整数
            dos.writeLong(239742398);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos!=null){
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值