Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed. Our assignment requires us to make an application to read and write customer contact information the user enters. Our assignment requires us to make GUI apps. I decided to make a JTable with 3 columns and 3 rows just for simplicity (FirstName, LastName, Age). I also made an empty file I named "contactsFile.txt" where I want java to store the customer information permanently. But I am so confused with the concept. I tried some sites for research but were not helping. Please help. Thanks in advance. Below is example of my code.
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import java.io.FileReader; import java.io.BufferedReader; import java.util.Scanner;
public class ContactInfoProgram extends JFrame {
public static void main (String[] args) { //declare member components JFrame frame = new JFrame ("Contacts"); String [] colNames = {"First Name", "Last Name", "Age"}; Object [][] data = new Object[30][5]; DefaultTableModel model = new DefaultTableModel(data, colNames); JTable contactsTable = new JTable(model);
contactsTable.setAutoResizeMode(JTable.AUTORESIZEOFF);
final int columnMI = 1;
TableColumn colMI = contactsTable.getColumnModel().getColumn(columnMI);
int columnMIwidth = 5;
colMI.setPreferredWidth(columnMIwidth);
// Set age column to 5 pixels wide final int columnAge = 3; TableColumn colAge = contactsTable.getColumnModel().getColumn(columnAge); int columnAgeWidth = 5; colAge.setPreferredWidth(columnAgeWidth); JScrollPane s = new JScrollPane(contactsTable); frame.setVisible(true); frame.setSize(600, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(contactsTable.getTableHeader(), BorderLayout.PAGE_START); frame.add(contactsTable); //get data entered by user in the columns and rows. need to tell java where to write //will need the name of the file. this is contactsFile.txt //First Name is in index position [0][0]?
} }
Here is an example of java swing that accepts the user input from the user through GUI components and store the data into the text file.
import java.io.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class Form extends JFrame{ JButton ADD,RETRIEVE; JPanel panel,pan; JLabel label1,label2,label3,label4,label5; final JTextField text1,text2,text3,text4,text5; Form() { label1 = new JLabel(); label1.setText("Employee Id:"); text1 = new JTextField(20); label2 = new JLabel(); label2.setText("Employee Name:"); text2 = new JTextField(20); label3 = new JLabel(); label3.setText("Employee Designation:"); text3 = new JTextField(20); label4 = new JLabel(); label4.setText("Employee Salary:"); text4 = new JTextField(20); label5 = new JLabel(); label5.setText("Employee Address:"); text5 = new JTextField(20); ADD=new JButton("ADD"); RETRIEVE=new JButton("RETRIEVE"); panel=new JPanel(new GridLayout(6,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(label3); panel.add(text3); panel.add(label4); panel.add(text4); panel.add(label5); panel.add(text5); panel.add(ADD); panel.add(RETRIEVE); add(panel,BorderLayout.CENTER); setTitle("FORM"); ADD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String id[]={text1.getText()}; String name[]={text2.getText()}; String des[]={text3.getText()}; String sal[]={text4.getText()}; String add[]={text5.getText()}; java.util.List<String> list = new ArrayList<String>(); for(int i = 0; i < id.length; i++) { String line = id[i]+" "+name[i]+" "+des[i]+" "+sal[i]+" "+add[i]; list.add(line); } writeToFile(list, "Employee.txt"); } }); RETRIEVE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ File file=new File("Employee.txt"); FileInputStream fis = null; BufferedInputStream bis = null; DataInputStream dis = null; String data=""; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); while (dis.available() != 0) { data+=dis.readLine()+"\n"; } System.out.println(data); JTextArea area =new JTextArea(10,25); JScrollPane sPane = new JScrollPane(area); area.setText(data); pan=new JPanel(); pan.add(sPane); JFrame frame=new JFrame(); frame.add(pan); frame.setSize(300,100); frame.setVisible(true); fis.close(); bis.close(); dis.close(); } catch(Exception e){} } }); } private static void writeToFile(java.util.List<String> list, String path) { BufferedWriter out = null; try { File file = new File(path); out = new BufferedWriter(new FileWriter(file,true)); for (String s : list) { out.write(s); out.newLine(); } out.close(); } catch(IOException e) {} } } class InsertToFile{ public static void main(String arg[]) { try { Form frame=new Form(); frame.setSize(300,300); frame.setVisible(true); } catch(Exception e) {} } }
To extract the data from text file and save it to jtable, visit the following links:
http://www.roseindia.net/tutorial/java/core/files/insertfiledataToJTable.html
http://www.roseindia.net/tutorial/java/swing/extractFileDataToJTable.html