JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
boolean doExport = true;
boolean overrideExistingFile = false;
File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
while (doExport && destinationFile.exists() && !overrideExistingFile) {
overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
if (!overrideExistingFile) {
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
} else {
doExport = false;
}
}
}
if (doExport) {
ExportTable(destinationFile);
}
}
public void ExportTable(File file)throws Exception
{
FileWriter writer = new FileWriter(file);
BufferedWriter bfw = new BufferedWriter(writer);
for(int i = 0 ; i < table.getColumnCount() ; i++)
{
bfw.write(table.getColumnName(i)+" ");
}
bfw.write("\n");
for (int i = 0 ; i < table.getRowCount(); i++)
{
for(int j = 0 ; j < table.getColumnCount();j++)
{
bfw.write((String)(table.getValueAt(i,j))+"\t");
}
bfw.write("\n");
}
bfw.close();
writer.close();
}
No comments:
Post a Comment