二进制I / O流

时间:2015-04-02 03:30:19

标签: java io javafx random-access

我有两个程序。一个输出一些信息:

import java.io.*;
import java.util.Arrays;
import java.util.Date;

public class Output {
    public static void main(String[] args) throws ClassNotFoundException,
            IOException {

        int[] numbers = { 1, 2, 3, 4, 5 };

        // Create an output stream for binary file
        try (ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("Exercise7_05.dat", true));) 
        {
            // Write to each type to the file
            output.writeDouble(5.5);
            output.writeObject(numbers);
            output.writeObject(new java.util.Date());
            output.writeUTF("Exercise7_05.dat");
        }
    }
}

另一个应该读取二进制输入并将其打印到控制台:

import java.io.*;
import java.util.Arrays;
import java.util.Date;

public class Input {
    public static void main(String[] args) throws ClassNotFoundException,
            IOException {

        /*
         * Create an input stream for binary file to be converted to something
         * readable
         */
        try (ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("Exercise7_05.dat"));) {

            // Read each object from binary file
            double doubleValue = input.readDouble();
            System.out.println("Double value: " + doubleValue);

            int[] newNumbers = (int[]) (input.readObject());
            System.out.println("Integers: " + Arrays.toString(newNumbers));

            Date date = (java.util.Date) (input.readObject());
            System.out.println("DateTime: " + date);

            String fileName = input.readUTF();
            System.out.println("File name: " + fileName);
        }
    }
}

我的问题是,当我尝试运行输入流时出现错误:

"线程中的异常" main" java.io.FileNotFoundException:Exercise7_05.dat(系统找不到指定的文件)     at java.io.FileInputStream.open(Native Method)     在java.io.FileInputStream。(未知来源)     在java.io.FileInputStream。(未知来源)     在application.Input.main(Input.java:18)"

0 个答案:

没有答案