如何在文本文件中的一行空格后拆分

时间:2014-07-05 19:18:27

标签: java excel jtable

我正在尝试将text / excel文件读入JTable。但它在行上排成一行,但不依赖于标题。我甚至没有得到标题。

public class TableTest extends JFrame {

    //create row and col as vector
    private Vector data;
    private Vector column;
    private final JPanel pane;
    private JTable table;

    //design the frame
    public TableTest() throws IOException {

        //set the title
        super("Table test");

        //set the frame size
        setSize(400, 400);

        //make frame visible
        setVisible(true);

        //close when click the cross button
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //set display layout 
        setLayout(new BorderLayout());

        //create panel to display table
        pane = new JPanel();
        pane.setLayout(new BorderLayout());

        //create table
        table = new JTable();

        //add it into panel
        pane.add(table);

        //add panel into frame
        add(pane);
        readFile(new DefaultTableModel(), table);
    }

    //read the file and populate table
    private void readFile(DefaultTableModel model, JTable table) throws FileNotFoundException, IOException {

        //create file reference which will be read
        FileReader reader = new FileReader("C:\\test.xls");

        //try buffered reader to read data from file reader
        try{

            //create buffered reader for read string stream
            BufferedReader br = new BufferedReader(reader);

            //store file data into string 
            String storedata;
            column = new Vector();
            data = new Vector();

            //add the header text of the file menually
            column.addElement("Day");
            column.addElement("Ramadan");
            column.addElement("Date");
            column.addElement("Sahree");
            column.addElement("Ifter");

            //get the column name automatically
            for (int i = 0; i < column.size(); i++) {
                model.addColumn(column.elementAt(i));
            }

            //read total data from the file and set it into data vector
            while ((storedata = br.readLine()) != null) {
                //add the row data into vector
                //String value[] = br.readLine().split(","); 
                data.addElement(storedata);

                //if found , end of the text then split it into new line use ,
                //if(storedata.endsWith(",")){
                  //  storedata.split(",");
                    //continue;
                //}

                ListIterator listIterator = data.listIterator();

                while(listIterator.hasNext()){
                    storedata = (String) listIterator.next();
                    //System.out.println((storedata));
                }
                data.addElement(storedata);
                //System.out.println(storedata);

                //finally add the data into table model
                model.addRow(data);
            }

            //populate data into table
            table.setModel(model);

            //refresh the table
            table.repaint();

            //debug
            System.out.println(column);
            System.out.println(data);

            //filanlly close the buffered reader
            br.close();

        } catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex, "Error about file reading !", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //read the file and populate table
    private void read(DefaultTableModel model, JTable table) throws FileNotFoundException, IOException {

        //try buffered reader to read data from file reader
        try{

            column = new Vector();

            //add the header text of the file menually
            column.addElement("Day");
            column.addElement("Ramadan");
            column.addElement("Date");
            column.addElement("Sahree");
            column.addElement("Ifter");

            //get the column name automatically
            for (int i = 0; i < column.size(); i++) {
                column.elementAt(i);
                model.addColumn(column);
            }

            data = new Vector();
            data.addElement("col 1");
            data.addElement("col 2");
            data.addElement("col 3");
            data.addElement("col 4");
            data.addElement("col 5");
            data.addElement("col 6");
            data.addElement("col 7");

            for(int i = 0; i<data.size(); i++){
                data.elementAt(i);
                model.addRow(data);
            }

            //populate data into table
            table.setModel(model);

            //refresh the table
            table.repaint();

            //debug
            System.out.println(column);
            System.out.println(data);

        } catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex, "Error about file reading !", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(TableTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) throws IOException {
        TableTest test = new TableTest();
    }
}

2 个答案:

答案 0 :(得分:0)

对于基于空格的分割字符串,请尝试

String.split("\\s");

答案 1 :(得分:0)

您也可以使用:

 String[] split = StringUtils.split(stringToSplit, " ");