首页 > 代码库 > DataInputStream和DataOutputStream

DataInputStream和DataOutputStream

 

操作基本数据类型的流

 

DataInputStream

 

DataOutputStream

 

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IntegerDemo {
	public static void main(String[] args) throws IOException {
		myWrite();

		myReader();
	}

	private static void myWrite() throws IOException {
		// TODO Auto-generated method stub
		// 创建数据输出流对象
		FileOutputStream fos = new FileOutputStream("E:\\zikao\\file\\cs.txt");
		DataOutputStream dos = new DataOutputStream(fos);

		// 写数据
		dos.writeByte(10);
		dos.writeShort(100);
		dos.writeInt(1000);
		dos.writeLong(10000);
		dos.writeFloat(12.34F);
		dos.writeDouble(12.56);
		dos.writeChar(‘a‘);
		dos.writeBoolean(true);

		// 释放资源
		dos.close();
	}

	private static void myReader() throws IOException {
		// TODO Auto-generated method stub
		// 创建数据输入流对象
		FileInputStream fis = new FileInputStream("E:\\zikao\\file\\cs.txt");
		DataInputStream dis = new DataInputStream(fis);

		// 读数据
		byte b = dis.readByte();
		short s = dis.readShort();
		int i = dis.readInt();
		long l = dis.readLong();
		float f = dis.readFloat();
		double d = dis.readDouble();
		char c = dis.readChar();
		boolean bl = dis.readBoolean();

		// 释放资源
		dis.close();

		System.out.println(b);
		System.out.println(s);
		System.out.println(i);
		System.out.println(l);
		System.out.println(f);
		System.out.println(d);
		System.out.println(c);
		System.out.println(bl);
	}
}

 

DataInputStream和DataOutputStream