zoukankan      html  css  js  c++  java
  • 34.3 转换流 InputStreamReader OutStreamReader

    转换流: 把字节输出流转换成字符输出流

    标准输入输出流:传输的对象是字节流 System.in 、 System.out

    标准输入输出流

    public static final InputStream in:字节输入流,用来读取键盘录入的数据
    public static final int x;
    InputStream is = System.in;
    Scanner sc = new Scanner(System.in);
    public static final PrintStream out:字节输出流,将数据输出到命令行 System.out.println();

      

    转换流

    import java.io.*;
    
    /*
     * 需求:读取键盘录入的数据,并输出到项目根目录下的a.txt文件中
     *
     * 数据源:读取键盘录入的数据    System.in(字节流)
     * 目的地:项目根目录下的a.txt    FileWriter(字符流)
     *
     *
     *
     * 转换流:需要把字节输入流转换成字符输入流,InputStreamReader
     * InputStreamReader(InputStream in)
     * InputStreamReader 是字节流通向字符流的桥梁
     */
    public class InputStreamReaderDemo {
        public static void main(String[] args) throws IOException {
    //        method();
    //        method2();
            method3();
    
    
    
        }
    
        //输入 字节转字符
        //输出 字符
        private static void method3() throws IOException {
            //创建输入流对象
            InputStream ips = System.in;
            InputStreamReader ipr = new InputStreamReader(ips);
    
            //创建输出流对象
            FileWriter fw = new FileWriter("io.txt");
    
            char[] chs = new char[1024];
            int len;
            while ((len=ipr.read(chs))!=-1) {
                fw.write(chs,0,len);
                fw.flush();
            }
    
            fw.close();
            ipr.close();
        }
    
        //输入 字节
        //输出 字符(输出时字节转字符串)
        private static void method2() throws IOException {
            InputStream ips = System.in;
            FileWriter fw = new FileWriter("io.txt");
    
            byte[] by = new byte[1024];
            int len;
            while ((len=ips.read(by))!=-1) {
                fw.write(new String(by,0,len)); //String(byte[] bytes) String类的构造方法把字节数据封装成字符串对象
                fw.flush();
            }
    
            fw.close();
            ips.close();
        }
    
        //字节输入
        //字节输出
        private static void method() throws IOException {
            //数据源
            InputStream ips = System.in;
            //目的地
            OutputStream fos = new FileOutputStream("io.txt");
    
            byte[] by = new byte[1024];
            int len;
            while ((len=ips.read(by))!=-1) {
                fos.write(by,0,len);
                fos.flush();
            }
    
            //释放资源
            ips.close();
            fos.close();
        }
    }
    import java.io.*;
    
    /*
     * 需求:读取项目根目录下的SystemInOutDemo.java,并输出到命令行
     * 数据源:项目根目录下的SystemInOutDemo.java    BufferedReader
     * 目的地:命令行    System.out
     *
     *
     *
     * 由于标准输出流是一个字节输出流,所以只能输出字节或者字节数组,但是我们读取到的数据则是字符串,如果想进行输出还需要转换成字节数组
     * 我们要想通过标准输出流输出字符串,把标准输出流转换成一种字符输出流即可,OutputStreamWriter
     *
     * OutputStreamWriter(OutputStream out) :转换流,把字节输出流转换成字符输出流
     *
     *
     */
    public class OutputStreamWriterDemo {
        public static void main(String[] args) throws IOException {
    //        method();
    
            //源端:文本文件
            /*FileReader fr = new FileReader("io.txt");
            BufferedReader br = new BufferedReader(fr);*/
            BufferedReader br = new BufferedReader(new FileReader("io.txt"));
    
            //目的地:命令行
            /*OutputStream ops = System.out;
            Writer opw = new OutputStreamWriter(ops);
            BufferedWriter bw = new BufferedWriter(opw);*/
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    
            String line;
            while((line=br.readLine())!=null) {
                bw.write(line);
                bw.newLine();
            }
    
            //释放资源
            br.close();
            bw.close();
    
    
    
        }
    
        //读取 字符
        // 输出 字符转字节(System.out是字节流)
        private static void method() throws IOException {
            BufferedReader br = new BufferedReader(new FileReader("io.txt"));
            OutputStream os = System.out;
    
            /*当读到最后一行后下一行返回null
            System.out.println(br.readLine());
            System.out.println(br.readLine());
            System.out.println(br.readLine());*/
            String line;
            while ((line=br.readLine())!=null) {
                os.write(line.getBytes());
                os.write("
    ".getBytes());
                os.flush();
            }
    
            os.close();
            br.close();
        }
    }
  • 相关阅读:
    函数的缺省参数和函数初始化示例以及布尔型参数的使用示例
    指针使用示例程序
    按值传递对象和按址传递对象
    详解js跨域
    CSS之BFC及其应用
    js图片预加载、有序加载
    12个非常有用的JavaScript技巧
    MySQL使用pt-online-change-schema工具在线修改1.6亿级数据表结构
    nodeJS实现一个在线填表应用
    浏览器的缓存机制
  • 原文地址:https://www.cnblogs.com/longesang/p/11320089.html
Copyright © 2011-2022 走看看