/* Written by Jen Chen Updated on March the 4th, 2003 Copyrighted by Jen Chen */ import java.io.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.Container; import java.awt.BorderLayout; import java.awt.event.*; import java.awt.Font; import java.util.*; public class myFileReader2 extends JFrame implements ActionListener{ private JMenuItem jmOpen, jmClose, jmSave, jmExit, jmAbout; private JTextArea jta = new JTextArea(); private JLabel jlab = new JLabel(); private JButton jbProdCsv = new JButton("Open Products.csv"); private JButton jbFilterCsv = new JButton("Open filteProducts.csv"); private JButton jbExit = new JButton("EXIT"); private JFileChooser jfc = new JFileChooser(); protected MyTable table; String inputFile; String outputFile; String file = null, line; String[][] data = new String[100][10]; //A 2-dimensional array to store JTable cells' data. String[] tmp = new String[10]; String[] columnNames = new String[10]; //A 1-dimensional array to store JTable column headers. BufferedReader br = null; StringTokenizer stToken; public myFileReader2() throws IOException{ setTitle("File Reader"); JMenuBar jmb = new JMenuBar(); //Create a menu bar. setJMenuBar(jmb); JMenu fileMenu = new JMenu("File"); //Create the "File" menu item. JMenu helpMenu = new JMenu("Help"); //Create the "Help" menu item. jmb.add(fileMenu); //Add the File-menu to the Menu bar. jmb.add(helpMenu); //Add the Help-menu to the Menu bar. //Add submenu-items to each drop down menu. fileMenu.add(jmOpen = new JMenuItem("Open")); fileMenu.add(jmClose = new JMenuItem("Close")); fileMenu.add(jmSave = new JMenuItem("Save")); fileMenu.addSeparator(); //Add the menu seperator. fileMenu.add(jmExit = new JMenuItem("Exit")); fileMenu.add(jmAbout = new JMenuItem("About")); DefaultTableModel dm = new DefaultTableModel(); //Create a default table. BufferedReader br = null; File out = new File("filterProducts.csv"); BufferedWriter bw = new BufferedWriter(new FileWriter(out)); try{ br = new BufferedReader(new FileReader("Products.csv")); line = br.readLine(); //read the 1st line of the csv file. //Set the column headers. columnNames = line.split(","); //Convert the contents of the 1st line into a //1-dimensinal array of 10 elements, then save it to the array columnNames[]. line = br.readLine(); //start reading into the records. //Create data for the table. int row = 0; while(line != null){ tmp = line.split(","); for (int col = 0; col < columnNames.length; ++col){ data[row][col] = tmp[col]; //store cells' data to the 2-dimensional array. } row++; line = br.readLine(); }//end of WHILE-statement. //} } //END of TRY-statement. catch(Exception e) { System.out.println(e); } // Configure the model with the data and column headers. dm.setDataVector(data, columnNames); // dm.insertRow(0, new Object[] {"1","2","3","4","5","6","7","8","9","10"}); // dm.insertRow(dm.getRowCount(), new Object[]{"1","2","3","4","5","6","7","8","9","10"}); //Remove the rows in a JTable. for(int i = 0; i < 100; i++){ dm.removeRow(99 - i); } //Now insert a new row into the JTable. dm.insertRow(0, new Object[] {"1","2","3","4","5","6","7","8","9","10"}); // Create the table. table = new MyTable(); // Connect the model to the table. table.setModel(dm); Container cont = getContentPane(); cont.setLayout(new BorderLayout()); jfc.setCurrentDirectory(new File(".")); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JScrollPane(table), BorderLayout.CENTER); cont.add(panel, BorderLayout.NORTH); JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayout(2, 5)); panel2.add(new JLabel()); panel2.add(jbProdCsv); panel2.add(jbFilterCsv); panel2.add(jbExit); panel2.add(new JLabel()); panel2.add(new JLabel()); panel2.add(new JLabel()); panel2.add(jlab); panel2.add(new JLabel()); panel2.add(new JLabel()); cont.add(panel2, BorderLayout.CENTER); jmOpen.addActionListener(this); jmClose.addActionListener(this); jmSave.addActionListener(this); jmExit.addActionListener(this); jmAbout.addActionListener(this); jbProdCsv.addActionListener(this); jbFilterCsv.addActionListener(this); jbExit.addActionListener(this); }//end of constructor. public static void main(String[] args) throws IOException{ myFileReader2 fileRead = new myFileReader2(); fileRead.setSize(700, 600); fileRead.setLocation(100, 100); fileRead.setVisible(true); fileRead.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }//end of main() public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if(e.getSource() == jbExit) System.exit(0); if(e.getSource() == jbProdCsv){ DefaultTableModel dm = new DefaultTableModel(); dm = (DefaultTableModel) table.getModel(); dm.setRowCount(0); dm.setColumnCount(0); try{ br = new BufferedReader(new FileReader("Products.csv")); line = br.readLine(); //read the column headers //Set the column headers. columnNames = line.split(","); line = br.readLine(); //start reading into the records. //Create data for the table. int row = 0; while(line != null){ tmp = line.split(","); for (int col = 0; col < columnNames.length; ++col){ data[row][col] = tmp[col]; } row++; line = br.readLine(); }//end of WHILE-statement. }//END of TRY-statement. catch(Exception er) { System.out.println(er); } // Configure the model with the data and column headers. dm.setDataVector(data, columnNames); // dm.addRow(new Object[]{"A","B","C","D","E","F","G","H","I","J"}); } if(e.getSource() == jbFilterCsv){ DefaultTableModel dm = new DefaultTableModel(); dm = (DefaultTableModel) table.getModel(); dm.setRowCount(0); dm.setColumnCount(0); File out = new File("newProducts.csv"); try{ BufferedWriter bw = new BufferedWriter(new FileWriter(out)); br = new BufferedReader(new FileReader("Products.csv")); if((line = br.readLine()) != null) bw.write(line + "\n"); while((line = br.readLine()) != null){ if(line.trim().endsWith("0")){ //testing condition: if matches then write the string tmp to the output file. bw.write(line + "\n"); } //end of IF-statement. } //end of the WHILE-loop. bw.close(); //Close the file writer so that the contents will be written to the new file. br = new BufferedReader(new FileReader("newProducts.csv")); line = br.readLine(); //read the column headers //Set the column headers. columnNames = line.split(","); int row = 0; while(line != null){ tmp = line.split(",");//Read into each line (record), then assign the elements to the //temporary array tmp[]. //Store cells' data into the 2-dimensional array data[][]. for (int col = 0; col < columnNames.length; ++col){ data[row][col] = tmp[col]; } row++; //increment the row count. line = br.readLine(); //Read into the next line (record). }//end of WHILE-statement. } //END of TRY-statement. catch(Exception er) { System.out.println(er); } //Now pass the column headers and cells' data to the JTable. dm.setDataVector(data, columnNames); } if(e.getSource() instanceof JMenuItem){ if("Open".equals(actionCommand)) open(); else if("Close".equals(actionCommand)) close(); else if("Save".equals(actionCommand)) save(); else if("About".equals(actionCommand)) JOptionPane.showMessageDialog(this, "Using File Reader", "File Reader window", JOptionPane.INFORMATION_MESSAGE); else if("Exit".equals(actionCommand)) System.exit(0); }//end of IF() }//end of ActionPerformed() private void open(){ if(jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) open(jfc.getSelectedFile()); //Open the chosen file. } private void open(File file){ try{ DefaultTableModel dm = new DefaultTableModel(); dm = (DefaultTableModel) table.getModel(); dm.setRowCount(0); dm.setColumnCount(0); try{ br = new BufferedReader(new FileReader(jfc.getSelectedFile())); line = br.readLine(); //read the column headers //Set the column headers. columnNames = line.split(","); line = br.readLine(); //start reading into the records. //Create data for the table. int row = 0; while(line != null){ tmp = line.split(","); for(int col = 0; col < columnNames.length; ++col){ data[row][col] = tmp[col]; } row++; line = br.readLine(); }//end of WHILE-statement. } //END of TRY-statement. catch(Exception er) { System.out.println(er); } // Configure the model with the data and column headers. dm.setDataVector(data, columnNames); } catch(Exception e){ jlab.setText("Error opening " + file.getName()); }//end of try{} and catch{} }//end of open(file) private void close(){ jta.setText(""); } //end of save() private void save(){ if(jfc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) save(jfc.getSelectedFile()); } //end of save() private void save(File file){ try{ BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); byte[] b = (jta.getText()).getBytes(); out.write(b, 0, b.length); out.close(); jlab.setText(file.getName() + "saved"); } catch(Exception e){ jlab.setText("Error saving " + file.getName()); }//end of try{} }// end of save(File file) public void setOutputFile(String outfileName){ this.outputFile = outfileName; } public String getOutputFile(){ return this.outputFile; } public void setFile(String fileName){ this.inputFile = fileName; } public String getFile(){ return this.inputFile; } }