JAVA IO - How To Export JTable Data To txt File In Java

JAVA - How To Set JTable Data To txt File In Java

__________________________________________________________________________

In this JAVA IO Tutorial we will see How To Write JTable Data To Text File In Java NetBeans .



Project Source Code:


package JavaDB_001;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Work extends JFrame{
     JTable table;
     JButton button;
         Object row[][] = {{"A1","B1","C1"},{"A2","B2","C2"},{"A3","B3","C3"},{"A4","B4","C4"}
                           ,{"A5","B5","C5"},{"A6","B6","C6"},{"A7","B7","C7"},{"A8","B8","C8"}
                            ,{"A9","B9","C9"},{"A10","B10","C10"}
                      };
    Object column[] = {"column 1","column 2","column 3"};
     public Work(){


         super("Export JTable Data To Text File");

         

         //Bind Data In JTable
         table = new JTable(row, column);




         JScrollPane js = new JScrollPane(table);
         js.setBounds(20,20,440,350);
         
         button = new JButton("Write JTable Values In Text File");
         button.setBounds(120, 380, 250, 30);
         
        button.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent e){
               try{
                //the file path
               File file = new File("C:\\Users\\samsng\\Desktop\\folder\\Text.txt");
               //if the file not exist create one
               if(!file.exists()){
                   file.createNewFile();
               }
               
               FileWriter fw = new FileWriter(file.getAbsoluteFile());
               BufferedWriter bw = new BufferedWriter(fw);
               
               //loop for jtable rows
               for(int i = 0; i < table.getRowCount(); i++){
                   //loop for jtable column
                   for(int j = 0; j < table.getColumnCount(); j++){
                       bw.write(table.getModel().getValueAt(i, j)+" ");
                   }
                   //break line at the begin 
                   //break line at the end 
                   bw.write("\n_________\n");
               }
               //close BufferedWriter
               bw.close();
               //close FileWriter 
               fw.close();
               JOptionPane.showMessageDialog(null, "Data Exported");
               
               }catch(Exception ex){
                   ex.printStackTrace();
               }
           }
        });

         add(button);
         add(js);
         setLayout(null);
         setSize(500, 500);
         getContentPane().setBackground(Color.decode("#bdb76b"));
         setLocationRelativeTo(null);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setVisible(true);
     }
     
     public static void main(String[] args){
         new Work();
     }
    }

OUTPUT:

Export JTable Rows Data To Text File
Export JTable Rows Data To Text File

File Result:

A1 B1 C1 
_________
A2 B2 C2 
_________
A3 B3 C3 
_________
A4 B4 C4 
_________
A5 B5 C5 
_________
A6 B6 C6 
_________
A7 B7 C7 
_________
A8 B8 C8 
_________
A9 B9 C9 
_________
A10 B10 C10 

_________




Share this

Related Posts

Previous
Next Post »

3 comments

comments
22 juin 2017 à 08:06 delete

its very hard to see....

Reply
avatar
22 juin 2017 à 08:58 delete

thanks for your feedback and i hope it's ok now

Reply
avatar
4 novembre 2017 à 09:12 delete

Hi. Good job. I have a problem with JTable can you help me anyway? My name is Marcos. Do you have email?

Reply
avatar