如何从文件中将记录加载到jTable中?

时间:2016-04-08 13:24:47

标签: java swing io randomaccessfile eofexception

我正在使用netbeans制作一个GUI程序,它应该是一个用于管理视频商店中记录的界面。

enter image description here enter image description here

这是界面。它是两个标签,一面允许一个人添加记录,另一面显示它们。当一个人添加记录时,它们会被添加到名为output的.dat文件中。我想使用.dat文件作为视频记录的永久存储区域,基本上我想要发生的是当加载GUI类时,程序加载.dat文件中的所有记录。我已经创建了我的代码,但是我收到了以下错误:

run:
java.io.EOFException
    at java.io.RandomAccessFile.readChar(RandomAccessFile.java:773)
    at videostore.BinaryFile.getString(BinaryFile.java:82)
    at videostore.BinaryFile.load(BinaryFile.java:116)
    at videostore.VideoStore.main(VideoStore.java:409)
Exception in thread "main" java.lang.NullPointerException
    at videostore.VideoStore.main(VideoStore.java:420)
/Users/(my Name)/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)

然后我会粘贴下面的所有相关代码。

在GUI类的main方法中,名为 VideoStore .java:

file = new BinaryFile("/Users/hanaezz/Desktop/output.dat");
        int length = file.length();
        int ID = 1;

        for (int xx = 0; xx < length; xx += file.getRecordLength()) {
            Video load = file.load(xx);
            String tempName = load.getVideoName();
            String tempProd = load.getProducer();
            String tempRat = load.getRating();
            String tempGenre = load.getGenre();
            short tempNum = load.getVidNum();
            float tempPrice = load.getvideoPrice(); 

            Object[] row = {ID, tempName, tempProd, tempGenre, tempRat, tempNum, tempPrice};


            model.addRow(row);

            ID++;
        }
VideoStore 构造函数类中的

public VideoStore() {
        initComponents();
        model = (DefaultTableModel) displayVideos.getModel();
    }

BinaryFile 类中:

private static final int RecordLength = 112;
public static Video load(int place){
        String name = "", prod="", rat="", genre="";

        float price = 1;
        short number = 1;
        try {
            raf.seek(place);
            name = getString(20);
            prod = getString(15);
            rat = getString(20);
            genre = getString(10);
            price = Float.parseFloat(getString(4));
            number = Short.parseShort(getString(4));

            writeString(20, name);
            writeString(15, prod);
            writeString(10, genre);
            writeString(4, VideoStore.vPrice.getText());
            writeString(4, VideoStore.vNumber.getText());
            writeString(4, rat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Video r = new Video(name, prod, genre, rat, number, price);
        return r;
    }

    public static int getRecordLength() throws IOException{
        return RecordLength;
    }

    public static int length() throws IOException {
        return (int)raf.length();
        }

最后,我的视频类:

    private static String videoName;
    private static String producer;
    private static String rating;
    private static String genre;
    private static short videoNumber;
    private static float videoPrice;

public Video(String a, String b, String c, String d, short e, float f){
    videoName = a;
    producer = b;
    rating = c;
    genre = d;
    videoNumber = e;
    videoPrice = f;
}

...然后是类中每个变量的mutator和accessor方法......

@Override
public String toString(){
    return videoName + "\t" + producer +
            "\t" + rating + "\t" + genre +
            "\t" + videoNumber + "\t" + videoPrice;
}

所以是的,我的问题是我无法弄清楚如何将文件中的记录加载到表格中。在我的代码中,我尝试使用一个循环,它将根据记录的大小迭代文件中的每个记录..但它似乎没有起作用。如果有人想查看我的完整代码或需要更多信息,请不要犹豫与我联系:)

1 个答案:

答案 0 :(得分:1)

首先,您应该使用更多面向对象的方法。

您的视频类只包含静态属性,如下所示:

public class Video implements Serializable{

    private String name;
    private String producer; //consider using an object for this
    private String rating; //consider using a numeric type for this
    private String genre; //consider using an object for this
    private int number;
    private double price;


    //getters and setters

}

检查Object-Oriented Programming Concepts

要添加新视频,您可以从图形界面获取用户输入,并使用它来创建视频对象。

Video video = new Video();

video.setName(nameTextField.getText());
//same for the other attributes

然后,您可以将所有视频保存在List

List<Video> videosList = new ArrayList<>();

videoList.add(video);

然后您可以serialize将您的列表添加到文件中。

try(FileOutputStream outputFile = new FileOutputStream("/path/to/file");
    ObjectOutputStream out = new ObjectOutputStream(outputFile)){

      out.writeObject(videoList);

} catch (IOException e1) {

      // Handle the exception

}

要从文件中回读列表,您需要对其进行反序列化:

try(FileInputStream inputFile = new FileInputStream("/path/to/file");
    ObjectInputStream in = new ObjectInputStream(inputFile)){

      videoList = (List<Video>)in.readObject();

} catch (IOException e1) {

      // Handle the exception

}