zoukankan      html  css  js  c++  java
  • 21(2)转换流 字节转字符,字符转字节 系统流

    OutputStreamWriter:字节输出流转成字符输出流
    public class OutputStreamWriterDemo {
        public static void main(String[] args) throws IOException {
            //主要作用将字节流转成字符流输出
            //要写出的数据先放到字符输出流,底层真正做数据传输的是字节输出流
            //字符输出流---字节输出流
            OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\12.txt"));
            //写出数据---字符串---字符 通过字符流写出数据
            osw.write("abc");
            //关流
            osw.close();
        }

    InputStreamReader :字节输入流转成字符输入流

    public static void main(String[] args) throws IOException {
            //底层从文件读取的是字节输入流
            //展示的时候使用字符输入流
            //字节流---字符流
            InputStreamReader isr=new InputStreamReader(new FileInputStream("D:\12.txt"));
            //读取数据---字符数组--字符---字符流
            char[] cs=new char[5];
            int len=-1;
            while((len=isr.read(cs))!=-1){
                System.out.println(new String(cs,0,len));
            }
        }

     

    系统流  
    系统流都是静态全局唯一所以不能关流
            //out---字节输出流的对象
            //黑色---表明输出结果
            System.out.println(1);
            //err---字节输出流的对象
            //红色---表明输出错误/异常
            System.err.println(1);
            //in---字节输入流
            new Scanner(System.in);
        public static void main(String[] args) {
            //
            Scanner sc1=new Scanner(System.in);
            String s1=sc1.nextLine();
            //关流
            sc1.close();   //系统流都是静态的---全局唯一所以关流了以后后面的会报异常
            //系统流都是静态的不能关流
            Scanner sc2=new Scanner(System.in);
            String s2=sc2.nextLine();
            System.out.println(s1);
            System.out.println(s2);
        }

    结果

    1
    Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Unknown Source)
        at cn.tedu.io.system.SystemDemo1.main(SystemDemo1.java:15)
     
  • 相关阅读:
    归一化与标准化的概念与区别
    深度学习中的优化器学习
    yolo3与yolo-tiny
    给tensorflow1.x estimator 添加timeline分析性能
    python 爬取百度图片
    TensorFlow 高性能数据输入管道设计指南
    TensorRT加速tensorflow模型(使用Tensorflow内置tensorRT,避免写自定义Plugin)
    21.Pod的limit和request和资源监控收集服务Heapster
    20.调度器,预选策略和优选策略
    8.docker的安全性
  • 原文地址:https://www.cnblogs.com/xuwangqi/p/11234239.html
Copyright © 2011-2022 走看看