package com.wkcto.chapter06.filterstream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* DataInputStream/DataOutputStream
* 可以读写带有数据格式的数据
* 不直接对数据源进行操作, 是处理流
* @author 蛙课网
*
*/
public class Test02 {
public static void main(String[] args) throws IOException {
//1) 保存数据
// writeData();
//2) 读取文件
readData();
}
//使用DataInputStream读取文件的内容
private static void readData() throws IOException {
InputStream in = new FileInputStream("d:/def.txt");
DataInputStream dis = new DataInputStream(in);
//读取的顺序要与写入的顺序一致
int num = dis.readInt();
double dd = dis.readDouble();
boolean flag = dis.readBoolean();
String text = dis.readUTF();
dis.close();
System.out.println("num=" + num + " ,dd=" + dd + " ,flag=" + flag + " ,text=" + text);
}
//使用DataOutputStream保存数据
private static void writeData() throws IOException {
OutputStream out = new FileOutputStream("d:/def.txt");
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(123); //保存整数
dos.writeDouble(3.14); //保存小数
dos.writeBoolean(true); //保存布尔
dos.writeUTF("wkcto"); //保存字符串
dos.close();
}
}