抱歉......我不清楚,我可以在txt文件中保存我的jtable中不同行的列名和行信息,现在我想将该文件加载到jtable中,这样当我重新打开时项目,它加载文件并将信息返回到jtable
保存代码:
private void saveMouseClicked(java.awt.event.MouseEvent evt) {
BufferedWriter bfw = null;
try {
bfw = new BufferedWriter(new FileWriter("Data.txt"));
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
for(int i = 0 ; i < model.getColumnCount() ; i++)
{
try {
bfw.write(model.getColumnName(i));
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
try {
bfw.write("\t");
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
}
for (int i = 0 ; i < model.getRowCount(); i++)
{
try {
bfw.newLine();
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
for(int j = 0 ; j < model.getColumnCount();j++)
{
try {
bfw.write((String)(model.getValueAt(i,j)));
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
try {
bfw.write("\t");
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
try {
bfw.close();
} catch (IOException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
}
加载代码:
private void LoadMouseClicked(java.awt.event.MouseEvent evt) {
BufferedReader bfr = null;
try {
bfr = new BufferedReader(new FileReader("Data.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(verprod.class.getName()).log(Level.SEVERE, null, ex);
}
String line;
while( (line = bfr.readLine() ) != null ) {
model.addRow( bfr.readLine().split("\t") );
model.addRow(new Object[] { bfr.readLine()});
model.addRow( line.split("\t") );
}
bfr.close();
}