我是Java的新手,我在GUI内创建了一个小表,我想从中打开一个.txt文件,以便用文件的内容填充该表。我试图从老师的例子中复制代码,但到目前为止,我还没有设法“捕获异常”。这是我的代码:
buddyLoadFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileDialog fd = new FileDialog(HauptFenster.this,
"Open File", FileDialog.LOAD);
fd.setDirectory(".");
fd.setVisible(true);
try {
String filename = fd.getDirectory()
+ fd.getFile();
buddyFileManager = new BuddyFileManager(filename);
buddyTableModel.setBuddies(buddyFileManager.load());
buddyTableModel.fireTableDataChanged();
}
catch (IOException ex) {
JOptionPane.showMessageDialog(
HauptFenster.this,
"error loading file", "Error",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception ex) {
JOptionPane.showMessageDialog(
HauptFenster.this,
"invalid format of file.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
});
不管我打开哪种文件,程序都会使用打开的文件中的字符串成功填充表格。我有没有机会得到我正在使用的代码的异常?
这是我的FileManager(以备不时之需):
public BuddyFileManager(String filename) {
fileName = filename;
}
public List<Buddies> load() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
StreamTokenizer strTokenizer = new StreamTokenizer(br);
strTokenizer.whitespaceChars(',', ',');
List<Buddies> buddyListe = new ArrayList<Buddies>();
非常感谢你。
MMTTMM
相关分类