zoukankan      html  css  js  c++  java
  • java IO流 (六) 其它的流的使用

    1. 标准的输入输出流:
    System.in:标准的输入流,默认从键盘输入
    System.out:标准的输出流,默认从控制台输出

    修改默认的输入和输出行为:
    System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入和输出的流。

    2. 打印流:
    PrintStream 和PrintWriter
    说明:
    提供了一系列重载的print()和println()方法,用于多种数据类型的输出
    System.out返回的是PrintStream的实例
    3. 数据流:
    DataInputStream 和 DataOutputStream
    作用:
    用于读取或写出基本数据类型的变量或字符串

    示例代码:
    /*
    练习:将内存中的字符串、基本数据类型的变量写出到文件中。

    注意:处理异常的话,仍然应该使用try-catch-finally.
    */

    @Test
    public void test3() throws IOException {
        //1.
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
        //2.
        dos.writeUTF("刘建辰");
        dos.flush();//刷新操作,将内存中的数据写入文件
        dos.writeInt(23);
        dos.flush();
        dos.writeBoolean(true);
        dos.flush();
        //3.
        dos.close();
    
    
    }
    /*
    将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
    
    注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!
    
     */
    @Test
    public void test4() throws IOException {
        //1.
        DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
        //2.
        String name = dis.readUTF();
        int age = dis.readInt();
        boolean isMale = dis.readBoolean();
    
        System.out.println("name = " + name);
        System.out.println("age = " + age);
        System.out.println("isMale = " + isMale);
    
        //3.
        dis.close();
    
    }
  • 相关阅读:
    整数反转
    最长公共前缀
    罗马数字转整数
    单点登录
    VMware Workstation虚拟机密钥
    Pytest 用例内部执行顺序
    判断是不是回文数
    python端口IP字符串是否合法
    python求二叉树深度
    有两个字符串类型的数字,实现一个方法将它们进行相加,并返回相加后的数值。
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13196448.html
Copyright © 2011-2022 走看看