簡體   English   中英

如何在文本文件中的一行中按空格分隔

[英]How to split after space in one line in text file

我正在嘗試將文本/ 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();
    }
}

要根據空格分割字符串,請嘗試,

String.split("\\s");

您還可以使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM