gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:05:35 26 4
gpt4 key购买 nike

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

enter image description here enter image description here

这是界面。它有两个选项卡,一侧允许用户添加记录,而另一侧选项卡则显示它们。当用户添加记录时,它们将被添加到名为“输出”的 .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();
}

最后,我的视频类(class):

    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;
}

...然后是类中每个变量的修改器和访问器方法...

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

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

最佳答案

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

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

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

}

关于java - 如何将记录从文件加载到 jTable 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36500779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com