package test;
import java.io.*;
public class Test {
public static void main(String args[]){
int[] data ={100,101,102,103,104,105};
int t;
try{
DataOutputStream out=new DataOutputStream(new FileOutputStream("dest.txt"));
for(int i=data.length-1;i>=0;i--)
out.writeInt(data[i]);
/*
* 关于DataOutputStream类中的几种方法
*
* void flush():清空此数据输出流
* int size():返回计数器 written 的当前值,即到目前为止写入此数据输出流的字节数
* void write(byte[] b, int off, int len):将指定 byte 数组中从偏移量 off 开始的 len 个字节写入基础输出流
* void write(int b):将指定字节(参数 b 的八个低位)写入基础输出流
* void writeBoolean(boolean v):将一个 boolean 值以 1-byte 值形式写入基础输出流
* void writeByte(int v):将一个 byte 值以 1-byte 值形式写出到基础输出流中
* void writeBytes(String s):将字符串按字节顺序写出到基础输出流中
* void writeChar(int v):将一个 char 值以 2-byte 值形式写入基础输出流中,先写入高字节
* void writeChars(String s):将字符串按字符顺序写入基础输出流
* void writeDouble(double v):将 double 参数转换为一个 long 值,然后将该 long 值以 8-byte 值形式写入基础输出流中,先写入高字节
* void writeFloat(float v):将 float 参数转换为一个 int 值,然后将该 int 值以 4-byte 值形式写入基础输出流中,先写入高字节
* void writeInt(int v):将一个 int 值以 4-byte 值形式写入基础输出流中,先写入高字节
* void writeLong(long v):将一个 long 值以 8-byte 值形式写入基础输出流中,先写入高字节
* void writeShort(int v):将一个 short 值以 2-byte 值形式写入基础输出流中,先写入高字节
* void writeUTF(String str):以与机器无关方式使用 UTF-8 修改版编码将一个字符串写入基础输出流
*/
out.close();
DataInputStream in=new DataInputStream(new FileInputStream("dest.txt"));
for(int i=data.length-1;i>=0;i--){
t=in.readInt();
/*
* 注意在DataInputStream类中的几个方法,其中readInt()方法是不含参数的,具体的几种方法如下:
*
* int read(byte[] b):从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中。
* int read(byte[] b, int off, int len):从包含的输入流中将最多 len 个字节读入一个 byte 数组中。
* boolean readBoolean():从包含的输入流中读取的 boolean 值的字节
* byte readByte():此输入流的下一个字节,以有符号 8 位 bit 的形式表示。
* char readChar():此输入流的下两个字节,将它们解释为一个 char
* double readDouble():此输入流的下八个字节,将它们解释为一个 double。
* float readFloat():此输入流的下四个字节,将它们解释为一个 float。
* void readFully(byte[] b, int off, int len):b - 存储读取数据的缓冲区。off - 数据的起始偏移量。len - 要读取的字节数。
* int readInt():此输入流的下四个字节,将它们解释为一个 int。
*/
System.out.println(" "+t);
}
in.close();
}catch(IOException e){
System.out.println(e.getMessage());
}
}
}
关于Java中IO包内的DataInputStream和DataOutputStream类的使用
最新推荐文章于 2021-04-17 15:36:01 发布