dataiotest.java

来自「是关于JAVA中的关于输入输出流的一些例子 可以让大家更好的了解输入输出流」· Java 代码 · 共 56 行

JAVA
56
字号
import java.io.*;

public class DataIOTest {
    public static void main(String[] args) throws IOException {

        // 创建数据输出流,前端套接文件流并以invoice1.txt为输出目的地。
        DataOutputStream out = new DataOutputStream(new
				   FileOutputStream("invoice1.txt"));

        //定义要保存的数据数组。
        double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
        int[] units = { 12, 8, 13, 29, 50 };
        String[] descs = { "Java T-shirt", "Java Mug", 
        					"Duke Juggling Dolls",
			                  "Java Pin", "Java Key Chain" };
	
	    //将prices,unites以及descs中的数据以Tab键为分割符保存在文件中。
		for (int i = 0; i < prices.length; i ++) {
            out.writeDouble(prices[i]);
            out.writeChar('\t');
            out.writeInt(units[i]);
            out.writeChar('\t');
            out.writeUTF(descs[i]);
            out.writeChar('\t');
        }
        out.close();

        // 创建数据输入流,将上面保存的文件再次打开并读取。
        DataInputStream in = new DataInputStream(new
				 FileInputStream("invoice1.txt"));

        double price;
        int unit;
        String desc;
        double total = 0.0;

        for (int i = 0; i < prices.length; i ++) {
                price = in.readDouble();
                in.readChar();       // 扔掉tab键
                unit = in.readInt();
                in.readChar();       // 扔掉tab键
                desc = in.readUTF();
                in.readChar();   // 扔掉tab键
                
                System.out.println("You've ordered " +
				    unit + " units of " +
				    desc + " at $" + price);
                total = total + unit * price;
        }
        
        System.out.println("For a TOTAL of: $" + total);
        in.close();
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?