JAVA-8 DataInputStream/DataOutputStream

其它类:

InputStream/OutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185306

ObjectInputStream/ObjectOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185460

ByteArrayInputStream/ByteArrayOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185609

BufferedInputStream/BufferedOutputStream:https://blog.csdn.net/sinat_27180253/article/details/82185662

 

DataInputStream 

DataInputStream 是数据输入流。它继承于FilterInputStream。

DataInputStream 是用来装饰其它输入流,它“允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型”。

DataInputStream 函数列表:

DataInputStream(InputStream in)
final int read(byte[] buffer, int offset, int length)
final int read(byte[] buffer)
final boolean readBoolean()
final byte readByte()
final char readChar()
final double readDouble()
final float readFloat()
final void readFully(byte[] dst)
final void readFully(byte[] dst, int offset, int byteCount)
final int readInt()
final String readLine()
final long readLong()
final short readShort()
final static String readUTF(DataInput in)
final String readUTF()
final int readUnsignedByte()
final int readUnsignedShort()

final int skipBytes(int count)

 

DataOutputStream

DataOutputStream 是数据输出流。它继承于FilterOutputStream。

DataOutputStream 是用来装饰其它输出流,“允许应用程序以与机器无关方式从底层输入流中读写基本 Java 数据类型”。

DataOutputStream 函数列表:

//将一个 boolean 值以 1-byte 值形式写入基础输出流。

void writeBoolean(boolean v) 

//将一个 byte 值以 1-byte 值形式写出到基础输出流中。

void writeByte(int v)

//将字符串按字节顺序写出到基础输出流中。

void writeBytes(String s)

//将一个 char 值以 2-byte 值形式写入基础输出流中,先写入高字节。

void writeChar(int v)

//将字符串按字符顺序写入基础输出流。

void writeChars(String s)

//使用 Double 类中的 doubleToLongBits 方法将 double 参数转换为一个 long 值,然后将该 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。

void writeDouble(double v)

//使用 Float 类中的 floatToIntBits 方法将 float 参数转换为一个 int 值,然后将该 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。

void writeFloat(float v)

//将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节。

void writeInt(int v)

//将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节。

void writeLong(long v)

//将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节。

void writeShort(int v)

//以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流。 

void writeUTF(String str)

 

代码:

package com.stydy.stream;

 

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.FileNotFoundException;

import java.lang.SecurityException;

 

public class DataStream {

    // 文件路径

    public static final String filePath = "D:" + File.separator + "ZZ" + File.separator + "stream.txt";

 

    public static void main(String[] args) {

        testDataOutputStream();

        testDataInputStream();

    }

 

    /**

     * DataOutputStream

     */

    private static void testDataOutputStream() {

 

        try {

            File file = new File(filePath);

            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));

 

            out.writeBoolean(true);

            out.writeByte((byte) 0x41);

            out.writeChar((char) 0x4243);

            out.writeShort((short) 0x4445);

            out.writeInt(0x12345678);

            out.writeLong(0x0FEDCBA987654321L);

 

            out.writeUTF("xiaowenzi(小蚊子)");

 

            out.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (SecurityException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    /**

     * DataInputStream

     */

    private static void testDataInputStream() {

 

        try {

            File file = new File(filePath);

            DataInputStream in = new DataInputStream(new FileInputStream(file));

 

            System.out.printf("byteToHexString(0x8F):0x%s\n", byteToHexString((byte) 0x8F));

            System.out.printf("charToHexString(0x8FCF):0x%s\n", charToHexString((char) 0x8FCF));

 

            System.out.printf("readBoolean():%s\n", in.readBoolean());

            System.out.printf("readByte():0x%s\n", byteToHexString(in.readByte()));

            System.out.printf("readChar():0x%s\n", charToHexString(in.readChar()));

            System.out.printf("readShort():0x%s\n", shortToHexString(in.readShort()));

            System.out.printf("readInt():0x%s\n", Integer.toHexString(in.readInt()));

            System.out.printf("readLong():0x%s\n", Long.toHexString(in.readLong()));

            System.out.printf("readUTF():%s\n", in.readUTF());

 

            in.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (SecurityException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    // 打印byte对应的16进制的字符串

    private static String byteToHexString(byte val) {

        /**

         * “byte类型转换成int类型”导致的问题。

         * 例如byte类型的0x8F是一个负数,它对应的2进制是10001111;

         * 将一个负数的byte转换成int类型时,执行的是有符号转型(新增位都填充符号位的数字)。

         * 0x8F的符号位是1,因为将它转换成int时,填充“1”;

         * 转型后的结果(2进制)是11111111 11111111 11111111 10001111,对应的16进制为0xffffff8f。

         * 因为当我们执行Integer.toHexString(val);时,返回的就是0xffffff8f。

         * 在Integer.toHexString(val & 0xff)中,相当于0xffffff8f & 0xff,得到的结果是0x8f。

         */

        return Integer.toHexString(val & 0xff);

    }

 

    // 打印char对应的16进制的字符串

    private static String charToHexString(char val) {

        // java中char是无符号类型,占两个字节。将char转换为int类型,执行的是无符号转型,新增为都填充0。

        return Integer.toHexString(val);

    }

 

    // 打印short对应的16进制的字符串

    private static String shortToHexString(short val) {

        return Integer.toHexString(val & 0xffff);

    }

}

输出

byteToHexString(0x8F):0x8f

charToHexString(0x8FCF):0x8fcf

readBoolean():true

readByte():0x41

readChar():0x4243

readShort():0x4445

readInt():0x12345678

readLong():0xfedcba987654321

readUTF():xiaowenzi(小蚊子)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章