Java数据流DataInputStream和DataOutputStream代码实例

Java数据流DataInputStream和DataOutputStream代码实例,第1张

Java数据流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;
import java.io.OutputStream;


public class DataStreamDemo {
    public static void main(String[] args) {
        write();
        read();

    }
    public static void read(){
        File file = new File("c://test/hh.dat");
        try {
            FileInputStream inputStream = new FileInputStream(file);
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            int i=dataInputStream.readInt();//按照写的顺序依次读出
            byte j=dataInputStream.readByte();
            String k=dataInputStream.readUTF();
            System.out.println("i="+i+" j="+j+" k="+k);
            dataInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void write(){
        File file = new File("c://test/hh.dat");
        try {
            OutputStream outputStream = new FileOutputStream(file);
            DataOutputStream dos = new DataOutputStream(outputStream);
            dos.writeInt(10);//写入四个字节
            dos.writeByte(1);//写入1个字节
            dos.writeUTF("中");

            dos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707649.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存