zoukankan      html  css  js  c++  java
  • 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

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

  • 相关阅读:
    sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用
    python 中feedParser
    Python中解码decode()与编码encode()与错误处理UnicodeDecodeError: 'gbk' codec can't decode byte 0xab
    Python中的排序方法sort(),sorted(),argsort()等
    Python 关于数组矩阵变换函数numpy.nonzero(),numpy.multiply()用法
    Python中where()函数的用法
    矩阵乘法np.dot()及np.multiply()以及*
    Python打开文件open()的注意事项
    给 Chrome浏览器 添加 Javascript小书签,查看当前页面全部加载的javascript文件及代码片段
    pycharm快捷键及中文说明【使用翻译工具一条一条翻译】
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14693354.html
Copyright © 2011-2022 走看看