欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

io之DataStream

程序员文章站 2022-05-28 16:52:41
...
package com.laien.io;

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

/**
 * 节点流:可以跟文件直接关联,如:FileOutputStream
 * 过滤流:用以包装节点流,如:DataOutputStream,BufferedOutputStream
 * 
 * @author Administrator
 *
 */
public class DataStreamTest {
	public static void main(String[] args) throws IOException {

		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
				new FileOutputStream("d:/data.txt")));
		byte b = 3;
		int i = 12;

		char ch = 'c';
		float f = 3.14f;

		// 写成二进制流
		dos.writeByte(b);
		dos.writeInt(i);
		dos.writeChar(ch);
		dos.writeFloat(f);

		dos.close();

		/***** 读数据 *********/
		DataInputStream dis = new DataInputStream(new BufferedInputStream(
				new FileInputStream("d:/data.txt")));
		
		
		//与写数据的顺序要一致		
		System.out.println(dis.readByte());
		System.out.println(dis.readInt());
		System.out.println(dis.readChar());
		System.out.println(dis.readFloat());
		
		dis.close();
	}

}

 

相关标签: io DataStream