5-数据流

package com.io;

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

import org.junit.Test;

/*
* 1、数据流有两个类DataInputStram,DataOutputStream,用于读取和写入java基本数据类型
*
* DataInputStream中的方法
boolean readBoolean() byte readByte()
char readChar() float readFloat()
double readDouble() short readShort()
long readLong() int readInt()
String readUTF() void readFully(byte[] b)

*/
public class 数据流 {

//用于写入基本数据类型
@Test
public void testData(){
DataOutputStream dos = null;
try {
FileOutputStream fos = new FileOutputStream(new File("data.txt"));
dos = new DataOutputStream(fos);

dos.writeUTF("吕亚飞");
dos.writeInt(123);
dos.writeBoolean(true);
} catch (IOException e) {
e.printStackTrace();
}

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

@Test
public void dataRead(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(new File("data.txt")));

String name = dis.readUTF();
int a = dis.readInt();
boolean b = dis.readBoolean();
System.out.println("name:"+name);
System.out.println("a:"+a);
System.out.println("b:"+b);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
}

posted @ 2015-12-07 15:09  浮白斋主人  阅读(179)  评论(0编辑  收藏  举报