【问题标题】:Reading/Writing to binary file读/写二进制文件
【发布时间】:2016-06-14 14:31:53
【问题描述】:

现在我必须将对象数组读/写到二进制文件中。我有代码,但由于某种原因,我收到了一个写入错误(我在 try catch 中创建的一个错误)。任何有关如何解决此问题的解决方案都值得赞赏。谢谢。

public class Trivia {
private String question;
private String answer;
private int points;

public Trivia() {
    question = " ";
    answer = " ";
    points = 0;
}

public String getQuestion() {
    return question;
}

public String getAnswer() {
    return answer;
}

public int getPoints() {
    return points;
}

public void setQuestion(String q) {
    question = q;
}

public void setAnswer(String a) {
    answer = a;
}

public void setPoints(int p) {
    points = p;
}

}

import java.io.*;
import java.util.*;

public class Driver  {

public static void main(String[] args) {
    String fileName = "trivia.dat";
    Trivia[] t = new Trivia[5];
    for (int i = 0; i < 5; i++) {
        t[i] = new Trivia();
    }

    t[0].setQuestion("How many states are in the US?");
    t[0].setAnswer("50");
    t[0].setPoints(1);

    t[1].setQuestion("What is the capital of Michigan?");
    t[1].setAnswer("Lansing");
    t[1].setPoints(1);

    t[2].setQuestion("How many senators come from each state?");
    t[2].setAnswer("2");
    t[2].setPoints(2);

    t[3].setQuestion("What is the largest state?");
    t[3].setAnswer("Alaska");
    t[3].setPoints(2);

    t[4].setQuestion("Who was the thrid president?");
    t[4].setAnswer("Thomas Jefferson");
    t[4].setPoints(3);

    ObjectOutputStream outputStream = null;

    try{
        outputStream = new ObjectOutputStream(new FileOutputStream("trivia.dat"));

    }catch(IOException e){
        System.out.println("Could not open file");
        System.exit(0);
    }

    try{
        outputStream.writeObject(t);
        outputStream.close();
    }catch(IOException e){
        System.out.println("Writing error");
        System.exit(0);
    }

    ObjectInputStream inputStream = null;

    try{
        inputStream = new ObjectInputStream(new FileInputStream("trivia.dat"));

    }catch(IOException e){
        System.out.println("File not found.");
        System.exit(0);
    }
     Trivia[] test = null;

     try{
         test = (Trivia[])inputStream.readObject();
     }catch(Exception e){
         System.out.println("Reading error");
         System.exit(0);
     }



}
}

【问题讨论】:

  • 而例外是......?
  • 向我们展示一些堆栈跟踪
  • 我上面说了。我收到“写作错误”。已经在代码}catch(IOException e){ System.out.println("Writing error"); System.exit(0); }
  • 异常的好处是它们包含错误消息和堆栈跟踪。您应该使用e.printStackTrace() 之类的信息将堆栈跟踪打印到控制台。因此,请更改您的代码并尝试理解错误消息。也许这可以直接解决您的问题。否则在此处发布堆栈跟踪并标记引发异常的行。

标签: java file binary


【解决方案1】:

您的Trivia 类必须实现Serializable 接口才能允许使用ObjectInputStream 编写它。

【讨论】:

  • 同样的事情,实施后我仍然得到我的“写作错误”\
  • 真的吗?嗯。我不能告诉你发生了什么事。这就是我把它改成的。 public class Driver implements Serializable
  • @ProgrammingGuy123 我说Trivia 必须实现接口NOT Driver ;-)
  • 啊,所以不再有错误了。但是,既然它实际运行了,我的 trivia.dat 文件即使在它应该被写入之后仍然是空的。
  • 你是如何检查它是空的?
【解决方案2】:

始终在您的 catch 子句中执行 printStackTrace() 以了解发生了什么。

您自己可能已经明白,您的 Trivia 类没有实现 java.io.Serializable

【讨论】:

  • "总是执行 printStackTrace()" 或者干脆不抓,让它冒泡
猜你喜欢
  • 2012-01-26
  • 2018-07-26
  • 2021-06-03
  • 2019-04-20
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
相关资源
最近更新 更多