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(小蚊子)

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