JAVA DataOutputStream和DataInputStream

來源:互聯網
上載者:User

標籤:put   not   ati   except   public   允許   指定   null   har   

是Java中輸入輸出資料流的裝飾類

DataOutputStream 資料輸出資料流允許應用程式以適當方式將基本 Java 資料類型寫入輸出資料流中。然後,應用程式可以使用資料輸入流將資料讀入。

主要方法:

void write(byte[] b,int off,int len);//將byte數組off角標開始的len個位元組寫到OutputStream 輸出資料流對象中。

void write(int b);//將指定位元組的最低8位寫入基礎輸出資料流。

void writeBoolean(boolean b);//將一個boolean值以1-byte形式寫入基本輸出資料流。

void writeByte(int v);//將一個byte值以1-byte值形式寫入到基本輸出資料流中。

void writeBytes(String s);//將字串按位元組順序寫入到基本輸出資料流中。

void writeChar(int v);//將一個char值以2-byte形式寫入到基本輸出資料流中。先寫入高位元組。

void writeInt(int v);//將一個int值以4-byte值形式寫入到輸出資料流中先寫高位元組。

void writeUTF(String str);//以機器無關的的方式用UTF-8修改版將一個字串寫到基本輸出資料流。該方法先用writeShort寫入兩個位元組表示後面的位元組數。

int size();//返回written的當前值

 

DataInputStream  允許應用程式以與機器無關方式從底層輸入資料流中讀取基本 Java 資料類型。應用程式可以使用資料輸出資料流寫入後由資料輸入流讀取的資料。

主要方法:

int read(byte[] b);//從輸入資料流中讀取一定的位元組,存放到緩衝數組b中。返回緩衝區中的總位元組數。

int read(byte[] buf,int off,int len);//從輸入資料流中一次讀入len個位元組存放在位元組數組中的位移off個位元組及後面位置。

String readUTF();//讀入一個已使用UTF-8修改版格式編碼的字串

String readLine();

boolean readBoolean;

int readInt();

byte readByte();

char readChar();

 

注意:DataInputStream與DataOutputStream必須成對出現  DataInputStream讀資料時,需要和DataOutputStream寫入時的順序一致,也不能跳過讀

package io;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class DataIOTest{    public static void main(String[] args)    {        writeData();        readData();    }    private static void writeData()    {        DataOutputStream dos = null;        try        {            dos = new DataOutputStream(new FileOutputStream(new File("dataoutput.txt")));        } catch (FileNotFoundException e1)        {            e1.printStackTrace();        }        try        {            dos.writeInt(123);            dos.writeByte(‘C‘);            dos.writeDouble(12.345);            dos.writeFloat((float) 1.23);            dos.writeUTF("this is a string");            dos.flush();        } catch (IOException e)        {            e.printStackTrace();        } finally        {            if (null != dos)            {                try                {                    dos.close();                } catch (IOException e)                {                    e.printStackTrace();                }            }        }    }    private static void readData()    {        DataInputStream dis = null;        try        {            dis = new DataInputStream(new FileInputStream(new File("dataoutput.txt")));        } catch (FileNotFoundException e)        {            e.printStackTrace();        }        try        {            System.out.println(dis.readInt());            System.out.println((char)dis.readByte());            System.out.println(dis.readDouble());            System.out.println(dis.readFloat());            System.out.println(dis.readUTF());        } catch (IOException e)        {            e.printStackTrace();        } finally        {            if (null != dis)            {                try                {                    dis.close();                } catch (IOException e)                {                    e.printStackTrace();                }            }        }    }}

 

JAVA DataOutputStream和DataInputStream

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.