我忙于编写FileWriter,而且编写文本文件的知识非常有限,我找到了一些我需要的示例,并以此创建了自己的代码。我在NetBeans工作。
目标:
将JTable内容导出到按下按钮的文本文件。
问题是:
bw.write(model.getValueAt(i, j));
预错误显示:No suitable method found for write(Output)...
这里发生了什么事?
--这是流程的工作方式:
1)管理员运行第一个运行配置。
2)管理员单击Add User {1}
(Apps.Settings.FTRun)

3)管理员通过输入字段创建用户。单击insert,应用程序创建一个用户on,然后将用户上传到数据库。另外,它还将用户名和密码添加到FTRun中的表中。--它应该添加元素,但它没有!(代码包括在下面)
Apps.UserManager.AddUser

4)表不填充,所以我在表中输入随机字符串。然后我点击。这会抛出NullPointerException
这是我的密码:
1)“出口法典”
2)填充表代码
出口代码
try {
File file = new File("C:/Program Files/DocuLoc/bin/export.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
TableModel model = jTable1.getModel();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
//Create your File Writer
bw.write(model.getValueAt(i, j));
}
}
bw.close();
JOptionPane.showConfirmDialog(this, "Table exported successfully!\nFile located at " + file);
} catch (Exception e) {
e.printStackTrace();
}填充表代码
try {
Apps.Settings.FTRun ftrun = new Apps.Settings.FTRun();
DefaultTableModel model = (DefaultTableModel) ftrun.jTable1.getModel();
model.addRow(new Object[]{UploadUName, UploadPwd});
ftrun.jTable1.enableInputMethods(true);
} catch (Exception e) {
e.printStackTrace();
}发布于 2014-04-04 16:13:55
我会用:
bw.write( model.getValueAt(i, j).toString() );它将写入可能存储在TableModel中的任何对象的字符串表示形式。
编辑:
NPE由bw.write(model.getValueAt(i,j).toString());行引起。
所以什么是null,"bw","model",来自getValue(.)的数据方法?
我猜这些数据,因为您可以使用如下代码:
Object data = model.getValueAt(I, j);
if (data == null)
System.out.println("null data at - " + I + " : " + j);
else
bw.write( data.toString() );然后,一旦您知道哪些单元格为空,您就会进行调查,以找出原因。
发布于 2014-04-04 15:13:09
没有一个BufferedWriter's write方法采用getValueAt返回的Object类型。你可以
bw.write((String)model.getValueAt(i, j));发布于 2014-04-04 19:40:46
感谢@camickr和@Reimeus在解决这个问题上的帮助!
此代码演示如何将JTable内容写入文本文件。
如果表中有值,则代码应该如下所示:
try {
File file = new File(filePathAndFileName); //Format: C:/.../.../file.txt
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//OPTIONAL: clear the file contents. Omitting this will continue
// writing the file
PrintWriter writer = new PrintWriter(file.getAbsoluteFile());
writer.print("");
writer.close();
//Start the writing process
TableModel model = jTable1.getModel();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
//Execute writer to write until all values in the table have been written
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
Object data = model.getValueAt(i, j);
bw.write(data.toString());
}
}
//OPTIONAL - If you have multiple columns and want to separate, use the
// following instead of the execution above
//FORMAT Column1 : Column2 : ...
//new line Column1 : Column2 : ...
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
Object data = model.getValueAt(i, j);
bw.write(data.toString());
//Custom coding here for the row. I used two columns
if (j == 0) {
bw.write(" : ");
} else {
}
}
bw.newLine();
}
//End the Writing Process
bw.close();
System.out.println("Writing complete");
}catch(Exception ex){
e.printStackTrace();
}https://stackoverflow.com/questions/22866426
复制相似问题