4-4 字节流之数据输入输出流
package com.yxt.haha;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
//7)DataOutputStream/DataInputStream对“流”功能的扩展,可以更加方便的镀组int,long,字符等类型数据
//DataOutputStream:writeInt()/writeDouble()/writeUTF()
public class DIOSdemo {
/**
* 往文件里写数据类型
* @param args
* @throws
*/
public static void DIS() throws IOException{
String file="demo/dos.dat";
DataOutputStream out=new DataOutputStream(new FileOutputStream(file));
out.writeInt(10);
out.writeInt(-10);
out.writeLong(10l);
out.writeDouble(10.5);
out.writeUTF("中国");//采用utf-8编码输出
out.writeChars("中国");//采用utf-16be编码
out.close();
IOUtil.printHex(file);
}
public static void DOS() throws IOException{
String file="demo/dos.dat";
DataInputStream in=new DataInputStream(new FileInputStream(file));
int i=in.readInt();
System.out.println(i);
i=in.readInt();
System.out.println(i);
long j=in.readLong();
System.out.println(j);
double k=in.readDouble();
System.out.println(k);
String d=in.readUTF();
System.out.println(d);
in.close();
}
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
DIS();
DOS();
}
}