![](/img/trans.png)
[英]How to display records in a JTable from an arraylist .TXT file in java MVC?
[英]How to load records into a jTable from a file?
我正在使用Netbeans制作一个GUI程序,该程序应该是用于管理视频存储中记录的接口。
这是界面。 它是两个选项卡,一侧允许一个人添加记录,而另一侧则显示它们。 当一个人添加记录时,它们将被添加到名为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类的主方法中,名为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();
}
最后,我的Video类:
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;
}
是的,我的问题是我不知道如何将记录从文件加载到表中。 在我的代码中,我尝试使用一个循环,该循环将根据记录的大小遍历文件中的每个记录。但是,它似乎没有用。 如果有人想查看我的完整代码或需要更多信息,请随时与我联系:)
首先,您应该使用一种更加面向对象的方法。
当您的视频类应如下所示时,它只包含静态属性:
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
}
检查面向对象的编程概念 。
要添加新视频,您需要从图形界面获取用户输入,然后使用它来创建Video对象。
Video video = new Video();
video.setName(nameTextField.getText());
//same for the other attributes
然后,您可以将所有视频保存在“ 列表”中 。
List<Video> videosList = new ArrayList<>();
videoList.add(video);
然后,您可以将列表序列化为文件。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.