Data Streams支持 primitive 数据类型以及字符串类型的字节流I/O,所有的Data Stream 类都实现了DataInput 与DataqOutput 这两个接口,在实现这两个接口的类中最为常用是:DataInputStream,DataOutputStream。官方文档的例子:
package io.datastream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreams {
static final String dataFile = "D:\\360云盘\\javase\\src\\io\\invoicedata";
static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
static final int[] units = { 12, 8, 13, 29, 50 };
static final String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
public static void main(String[] args) throws IOException {
DataOutputStream out = null;
try {
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
for (int i = 0; i < prices.length; i++) {
out.writeDouble(prices[i]);
out.writeInt(units[i]);
out.writeUTF(descs[i]);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
// ========================= Read the file ============
DataInputStream in = null;
double price;
int unit;
String desc;
double total = 0.0;
try {
in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
try {
while (true) {
price = in.readDouble();
unit = in.readInt();
desc = in.readUTF();
System.out.format("You ordered %d" + " units of %s at $%.2f%n", unit, desc, price);
total += price * unit;
}
} catch (EOFException e) {
e.printStackTrace();
}
System.out.format("Total amount %.2f", total);
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
这段代码是先将3个分别存放着相同个数,不同primitive类型或字符串类型的数组的数据写到invoicedata文件中,然后再将这些数据从文件中读出。上面这段代码的运行结果为:
运行结果1:
You ordered 12 units of Java T-shirt at $19.99
You ordered 8 units of Java Mug at $9.99
You ordered 13 units of Duke Juggling Dolls at $15.99
You ordered 29 units of Java Pin at $3.99
You ordered 50 units of Java Key Chain at $4.99
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at io.datastream.DataStreams.main(DataStreams.java:51)
Total amount 892.88
You ordered 8 units of Java Mug at $9.99
You ordered 13 units of Duke Juggling Dolls at $15.99
You ordered 29 units of Java Pin at $3.99
You ordered 50 units of Java Key Chain at $4.99
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at io.datastream.DataStreams.main(DataStreams.java:51)
Total amount 892.88
上面得到的结果都是正常的,但是如果将上面的代码稍作改动:
package io.datastream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreams {
static final String dataFile = "D:\\360云盘\\javase\\src\\io\\invoicedata";
static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
static final int[] units = { 12, 8, 13, 29, 50 };
static final String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };
public static void main(String[] args) throws IOException {
DataOutputStream out = null;
DataInputStream in = null;
double price;
int unit;
String desc;
double total = 0.0;
try {
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));
for (int i = 0; i < prices.length; i++) {
out.writeDouble(prices[i]);
out.writeInt(units[i]);
out.writeUTF(descs[i]);
}
// ========================= Read the file =====================
in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
try {
while (true) {
price = in.readDouble();
unit = in.readInt();
desc = in.readUTF();
System.out.format("You ordered %d" + " units of %s at $%.2f%n", unit, desc, price);
total += price * unit;
}
} catch (EOFException e) {
e.printStackTrace();
}
System.out.format("Total amount %.2f", total);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
将读取文件的代码移动写入文件代码片段后面,而且是在同一个try里面,也就是在outputstream关闭之前读取,其结果:
运行结果2
java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at io.datastream.DataStreams.main(DataStreams.java:42)
Total amount 0.00
也就是读取文件的代码并没有读到希望读到的数据。
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readLong(DataInputStream.java:416)
at java.io.DataInputStream.readDouble(DataInputStream.java:468)
at io.datastream.DataStreams.main(DataStreams.java:42)
Total amount 0.00
所以我猜测再输出流关闭之前对文件的修改,输入流是读取不到了?
不知道是不是这样?请懂的多多赐教。
后来发现,在输出流向文件写入完以后调用一下flush方法,这样DataInputStream 就可以读取到文件的内容了,其中,flush方法就是把数据流从内存刷新到文件中。