• java基础:IO流之输入输出流、打印流、数据流


    输入输出流

    简介

    System.inSystem.oult分别代表了系统标准的输入和输出设备。

    默认输入设备是:键盘,输出设备是:显示器

    System.in的类型是InputStream

    System.out的类型是PrintStream,其是FilterOutputStream的子类

    重定向:通过System类的setln,setOut方法对默认设备进行改变。

    • public static void setln(InputStream in)
    • public static void setOut(PrintStream out)

    System.in使用

    需求:从键盘输入字符串,要求将读取到的整行字符串都转成大写,然后继续操作,直至输入”e“时,退出程序。

    思路:键盘输入考虑使用System.in输入字节流,整行读取字符串可以使用BufferedReader输入字符流,而字节流转换成字符流,可以是使用InputStreamReader转换流,代码如下:

        //字节流:System.in
        public static void main(String[] args) {
            try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))){
                while (true){
                    String line = br.readLine();
                    if("e".equalsIgnoreCase(line)) break;
                    String upper = line.toUpperCase();
                    System.out.println(upper);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    

    测试效果:

    image-20210423111349062

    打印流

    简介

    实现将基本数据类型的数据格式转化为字符串输出

    打印流:PrintStream和PrintWriter

    • 提供了一系列重载的print()和println()方法,用于多种数据类型的输出
    • PrintStream和PrintWriter的输出不会抛出IOException异常
    • PrintStream和PrintWriter有自动flush功能
    • PrintStream打印的所有字符都使用平台的默认字符编码转换为字节。
    • 在需要写入字符而不是写入字节的情况下,应该使用PrintWriter类。
    • System.out返回的是PrintStream的实例
        @Test
        public void test1(){
            try(FileOutputStream fos = new FileOutputStream(new File("D:\print.txt"));
                //创建打印输出流,设置为自动刷新模式
                PrintStream ps = new PrintStream(fos, true)){
                //将标准输出流改成文件
                System.setOut(ps);
                for (int i = 0; i < 100; i++) {
                    System.out.print((char)i);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    

    image-20210423115502021

    数据流

    为了方便地操作Java语言地基本数据类型和String数据,可以使用数据流。

    数据流有两个类:(用于读取和写出基本数据类型、String类的数据)

    • DataInputStream和DataOutputStream
    • 分别套接在InputStream和OutputStream子类的流上

    DataInputStream的方法:

    image-20210423130418290

    DataOutputStream的方法:

    image-20210423130451345

    测试:

        @Test
        public void test(){
            try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("DataOutInStream.txt"))) {
                dos.writeInt(1998);
                dos.writeUTF("我是中国人");
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    
        /**
         * @author wen.jie
         * @date 2021/4/23 13:12
         * 将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中
         *
         */
        @Test
        public void test2(){
            try(DataInputStream dis = new DataInputStream(new FileInputStream("DataOutInStream.txt"))){
                System.out.println(dis.readInt());
                System.out.println(dis.readUTF());
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    

    先运行test方法,会生成一个文件,再用test2读取

    image-20210423131459670

    注意:读取不同类型的数据的顺序要与当初写入文件时,保存数据的顺序一致!否则会抛异常。

  • 相关阅读:
    跨域和表单重复
    Socket
    Redis(基本数据类型和使用Java操作Redis)
    初识Git
    SpringCloud一(eureka)
    SpringBoot3(springboot_jdbctemplate以及MyBatis和Dubbo整合)
    SpringBoot2(thymeleaf模板jsp页面和jpa)
    SpringBoot
    SpringBoot小型进销存系统
    MyBatis与SpringBoot整合案例
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14693354.html
Copyright © 2020-2023  润新知