• How do I use DataInputStream and DataOutputStream?


    DataOutputStream and DataInputStream give us the power to write and read primitive data type to a media such as file. Both of this class have the corresponding method to write primitive data and read it back.

    Using this class make it easier to read integer, float, double data and others without needing to interpret if the read data should be an integer or a float data. Let's see our code below.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    package org.kodejava.example.io;
      
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.DataOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
      
    public class PrimitiveStreamExample {
      
        public static void main(String[] args) {
            //
            // Prepares some data to be written to a file.
            //
            int cityIdA = 1;
            String cityNameA = "Green Lake City";
            int cityPopulationA = 500000;
            float cityTempA = 15.50f;
      
            int cityIdB = 2;
            String cityNameB = "Salt Lake City";
            int cityPopulationB = 250000;
            float cityTempB = 10.45f;
      
            try {
                //
                // Create an instance of FileOutputStream with cities.dat
                // as the file name to be created. Then we pass the input
                // stream object in the DataOutputStream constructor.
                //
                FileOutputStream fos = new FileOutputStream("cities.dat");
                DataOutputStream dos = new DataOutputStream(fos);
      
                //
                // Below we write some data to the cities.dat.
                // DataOutputStream class have various method that allow
                // us to write primitive type data and string. There are
                // method called writeInt(), writeFloat(), writeUTF(),
                // etc.
                //
                dos.writeInt(cityIdA);
                dos.writeUTF(cityNameA);
                dos.writeInt(cityPopulationA);
                dos.writeFloat(cityTempA);
      
                dos.writeInt(cityIdB);
                dos.writeUTF(cityNameB);
                dos.writeInt(cityPopulationB);
                dos.writeFloat(cityTempB);
      
                dos.flush();
                dos.close();
      
                //
                // Now we have a cities.dat file with some data in it.
                // Next you'll see how easily we can read back this
                // data and display it. Just like the DataOutputStream
                // the DataInputStream class have the corresponding
                // read methods to read data from the file. Some of
                // the method names are readInt(), readFloat(),
                // readUTF(), etc.
                //
                FileInputStream fis = new FileInputStream("cities.dat");
                DataInputStream dis = new DataInputStream(fis);
      
                //
                // Read the first data
                //
                int cityId1 = dis.readInt();
                System.out.println("Id: " + cityId1);
                String cityName1 = dis.readUTF();
                System.out.println("Name: " + cityName1);
                int cityPopulation1 = dis.readInt();
                System.out.println("Population: " + cityPopulation1);
                float cityTemperature1 = dis.readFloat();
                System.out.println("Temperature: " + cityTemperature1);
      
                //
                // Read the second data
                //
                int cityId2 = dis.readInt();
                System.out.println("Id: " + cityId2);
                String cityName2 = dis.readUTF();
                System.out.println("Name: " + cityName2);
                int cityPopulation2 = dis.readInt();
                System.out.println("Population: " + cityPopulation2);
                float cityTemperature2 = dis.readFloat();
                System.out.println("Temperature: " + cityTemperature2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    The generated result of our program are:

    City Id: 1
    City Name: Green Lake City
    City Population: 500000
    City Temperature: 15.5
    
  • 相关阅读:
    SSE图像算法优化系列十七:多个图像处理中常用函数的SSE实现。
    SSE图像算法优化系列十六:经典USM锐化中的分支判断语句SSE实现的几种方法尝试。
    Tone Mapping算法系列二:一种自适应对数映射的高对比度图像显示技术及其速度优化。
    SSE图像算法优化系列十五:YUV/XYZ和RGB空间相互转化的极速实现(此后老板不用再担心算法转到其他空间通道的耗时了)。
    Android项目之JSON解析(3种解析技术详解)
    Android 图片文字识别DEMO(基于百度OCR)
    再谈如何将android studio项目转换成eclipse
    集成百度OCR
    异步任务AsyncTask使用解析
    Nginx+IIS,Asp.Net 服务器配置
  • 原文地址:https://www.cnblogs.com/qingblog/p/2550448.html
Copyright © 2020-2023  润新知