日期:2014-05-19 浏览次数:20682 次
/**
*
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author Administrator
*
*/
public class BinaryUtil {
public static void main(String[] args) throws IOException {
DataOutputStream out = new DataOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\a.txt"));
double[] prices = { 19.99, 9.99, 15.99, 3.99};
int[] units = { 12, 8, 13, 29 };
String[] descs = { "Java", "Source ", "and","Support."};
for (int i = 0; i < prices.length; i++) {
out.writeDouble(prices[i]);
out.writeChar('\t');
out.writeInt(units[i]);
out.writeChar('\t');
out.writeChars(descs[i]);
out.writeChar('\n');
}
out.close();
// read it in again
DataInputStream in = new DataInputStream(new FileInputStream(
"C:\\Users\\Administrator\\Desktop\\a.txt"));
double price;
int unit;
String desc;
double total = 0.0;
try {
while (true) {
price = in.readDouble();
in.readChar(); // throws out the tab
unit = in.readInt();
in.readChar(); // throws out the tab