集册 Java实例教程 使用DataOutputStream和DataInputStream保存和读取各种数据类型值

使用DataOutputStream和DataInputStream保存和读取各种数据类型值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

418
使用DataOutputStream和DataInputStream保存和读取各种数据类型值

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.DataInputStream;//nowjava 提供

import java.io.DataOutputStream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.IOException;

import java.io.EOFException;


public class DataStreams {

    static final String dataFile = "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" };
            /**
             from
            * nowjava.com - 时代Java 
            **/


    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]);

            }

        } finally {

            out.close();

        }


        DataInputStream in = null;

        double total = 0.0;

        try {

            in = new DataInputStream(new BufferedInputStream(

                    new FileInputStream(dataFile)));


            double price;

            int unit;

            String desc;


            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 += unit * price;

                }

            } catch (EOFException e) {

            }

            System.out.format("For a TOTAL of: $%.2f%n", total);

        } finally {

            in.close();

        }

    }

}


展开阅读全文