0

I'm supposed to create a program that displays an editable CSV in a spreadsheet format, then pulls and appends information from an LDAP source into said spreadsheet and saves it. As it is a lot to handle right off the bat, I'm taking it in small chunks at a time.

Not even going to bother with the LDAP bits yet, I just want to get the Load > Edit > Save bits to work for now.

I figure that if I play around with opening, displaying, and saving .TXT files first I can easily transfer what I learn to later when I implement the .CSV portion. However, I am having difficulties making the Save portion work right (read: at all), and I got a bit of help from reading other posts on here, so I figured I'd ask.

Note: the Oracle Java tute for JFileChooser doesn't cover saving except to barely touch on how to start implementing it, not how it actually functions.

a link to my code: http://pastebin.com/tWnYrwgM

The code I need the most help with currently:

private void SaveActionPerformed(java.awt.event.ActionEvent evt) {                                    
//TODO
}

I don't have anything in there currently because I never actually got anything to work when I Built, Ran, or Debugged the project when I did have code there.

I guess what I am trying to say is that I am having trouble writing to file.

4

1 回答 1

0

好的,这将是您需要做的示例实现。请注意,虽然这适用于基本情况,但还不够。使用已建立的 CSV 库会做得更好,这仅用于教育目的。

JTable myTable; 
public void actionPerformed(ActionEvent e) {
    try {
        TableModel model = myTable.getModel(); // the table model contains all the data. Read its JavaDoc API it is enlightend when you work with JTables and you can do lots of neat stuff with it.
        JFileChooser fc = new JFileChooser();
        if(fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // no save when the user canceled.
            File target = fc.getSelectedFile();
            FileWriter fWri = new FileWriter(target); // this will create the file, but not any parent directories that might not exist. If there are parent directories that don't exist, this line will throw a FileNotFoundException.
            for(int ii = 0; ii < model.getRowCount(); ii++) {
                StringBuilder lineValue = new StringBuilder();
                for(int jj = 0; ii < model.getColumnCount(); jj++) {
                    lineValue.append(model.getValueAt(ii, jj));
                    lineValue.append(",");
                }
                lineValue.substring(0, lineValue.length() -1); // cut away the last ,
                fWri.write(lineValue + "\r\n"); // the RFC suggests CRLF, but don't be surprised when you get a CSV from somewhere that has only LF or something. Its because of lines like this that you want a library handle it for you.
            }
            fWri.flush();// in fact, this and the next line should go to a 'finally' block. Read about correct and clean file writing to learn more.
            fWri.close();
        }
    } catch (IOException ioex) {
         // file not found, cant be written to, etc. You handle it here.
    }
}
于 2012-07-25T16:42:43.840 回答