DataInputStream和DataOutputStream

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStream {
    public static void main(String[] args) throws IOException {
//        write("test5.txt");
//        read("test5.txt");
        readFromBuffer(writeToBuffer());
    }

    //write to local file
    public static void write(String path) throws IOException{
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
        out.writeInt(1);
        out.writeShort(2);
        out.writeLong(1);
        out.writeUTF("Some text");
        out.flush();
        out.close();
    }

    //read from local file
    public static void read(String path) throws IOException{
        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));

        System.out.println(in.readInt());
        System.out.println(in.readShort());
        System.out.println(in.readLong());
        System.out.println(in.readUTF());
        in.close();
    }

    //write to byte array
    public static byte[] writeToBuffer() throws IOException{
        byte[] buff = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(bos));
        out.writeInt(1);
        out.writeShort(2);
        out.writeLong(1);
        out.writeUTF("Some text");
        out.flush();
        out.close();
        buff = bos.toByteArray();
        bos.close();
        return buff;
    }

    //read from byte array
    public static void readFromBuffer(byte[] b) throws IOException{
        DataInputStream in = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(b)));

        System.out.println(in.readInt());
        System.out.println(in.readShort());
        System.out.println(in.readLong());
        System.out.println(in.readUTF());
        in.close();
    }

}
时间: 2024-10-25 21:42:57

DataInputStream和DataOutputStream的相关文章

数据流: DataInputStream 和 DataOutputStream

/* * 1.数据流: DataInputStream 和 DataOutputStream 一对. * 1) DataInputStream 数据的字节输入流: DataOutputStream 数据的字节输出流. * 2) 功能: 实现八种基本类型数据的输入/输出. 同时,也可实现字符串的输入/输出. * 3) 特点: 八种基本类型的数据在输入/输出时,会保持类型不变. * 因此,这种流特别适合在网络上实现基本类型数据和字符串的传递. * * 4) 方法: * readByte() writ

java下DataInputStream与DataOutputStream写入数据的同时写入数据类型

package cn.stat.p2.demo; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class datastreamdemo {

DataInputStream和DataOutputStream用法细节探讨

DataInputStream和DataOutputStream都是Java中输入输出流的装饰类,用起来很方便.今天就来讨论一下使用该类时候遇到的编码问题. package com.vincent.example; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import jav

DataInputStream和DataOutputStream使用方法细节探讨

DataInputStream和DataOutputStream都是Java中输入输出流的装饰类,用起来非常方便.今天就来讨论一下使用该类时候遇到的编码问题.  package com.vincent.example; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import j

【Java】IO流--数据流--DataInputStream、DataOutputStream

DataInputStream 和 DataOutputStream1) 提供了可以存取所有 Java 基础类型数据(如:int,double等)和 String 的方法.2) 处理流,只针对字节流,二进制文件3) 输入流链和输出流链4) 注意:只要关闭上层流即可 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.

java io流 数据流 DataInputStream、DataOutputStream、ByteArrayInputStream、ByteArrayOutputStream

例子程序: package io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; public class TestDataStream { public static void main(String[] a

类型流DataInputstream 和DataOutputStream的用法

package test; import java.io.*; import java.sql.SQLClientInfoException; import java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.z

JAVA DataOutputStream和DataInputStream

是Java中输入输出流的装饰类 DataOutputStream 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中.然后,应用程序可以使用数据输入流将数据读入. 主要方法: void write(byte[] b,int off,int len);//将byte数组off角标开始的len个字节写到OutputStream 输出流对象中. void write(int b);//将指定字节的最低8位写入基础输出流. void writeBoolean(boolean b);/

DataOutputStream and DataInputStream

        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("a.txt")));         dos.writeInt(5);         dos.writeBoolean(true);         dos.flush();         dos.close();         DataInputStream dis = new Da