简体   繁体   中英

How to modify a txt file using jframe in java

I have data saved in txt file. I have loaded them in a jtable in one of my forms and I want to be able to edit the txt file using buttons in the GUI form. Also if you can tell me why I have empty lines appearing in the jtable that will be very helpful.

    private void delbtnActionPerformed(java.awt.event.ActionEvent evt) {                                       
    
}                                      

private void modifybtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
}                                         

private void newpassActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void newusernameActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
} 

here is the view button code

    private void viewbtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
    try{
        BufferedReader br = new BufferedReader(new FileReader("logindata.txt"));
        String firstLine = br.readLine().trim();
        String[] columnsName = firstLine.split("/");
        DefaultTableModel model = (DefaultTableModel)datatable.getModel();
        model.setColumnIdentifiers(columnsName);
       
        Object[] tableLines = br.lines().toArray();
        
        for(int i = 0; i < tableLines.length; i++)
        {
            String line = tableLines[i].toString().trim();
            String[] dataRow = line.split(",");
            model.addRow(dataRow);
        }
    }
    catch(Exception ep){
        Logger.getLogger(people_info.class.getName()).log(Level.SEVERE, null, ep);
    }
} 

here is how I saved data in txt file

    private void submitbtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String user = usertxt.getText();
    String pass = passwordtxt.getText();
    
    try
    {
        FileOutputStream ldata = new FileOutputStream("logindata.txt", true);
        PrintWriter printer = new PrintWriter(ldata);
        
        printer.println(user+","+pass+","+","+",");
        JOptionPane.showMessageDialog(null,"success, please login");
        printer.close();
        
        this.setVisible(false);
        login lg = new login();
        lg.setVisible(true);
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,"Error, please retry");
    }        
} 

这是 GUI 的样子

after model.setColumnIdentifiers(columnsName); add model.setRowCount(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM