简体   繁体   English

阅读txt并添加jtable

[英]Read txt and add in jtable

I am new to java and I need to create a program that reads text from a txt file and puts it into a jtable, the format in the txt file will be like "b3:42". 我是Java的新手,我需要创建一个程序来从txt文件读取文本并将其放入jtable,该txt文件中的格式类似于“ b3:42”。 And I need to read those and put b3 in the first column of the table and 42 in the next column. 我需要阅读这些内容,并将b3放在表的第一列中,然后将42放在下一列中。 Could you help me ? 你可以帮帮我吗 ? And sorry for my english. 对不起,我的英语。

you can refer below code . 您可以参考以下代码。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.*;    
public class HelloWorld{
    private static final String FILENAME = "input.txt";
     public static void main(String []args){       
        BufferedReader br = null;
        FileReader fr = null;
        try {
            fr = new FileReader(FILENAME);
            br = new BufferedReader(fr);

            String s;
            String[][] data = new String[2][2];

            br = new BufferedReader(new FileReader(FILENAME));
            int i = 0;
            while ((s = br.readLine()) != null) {
                 data[i]= s.split(":");
                i++;
            }

           String column[]={"COL1","COL2"};
            JTable jt=new JTable(data,column);    
        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (br != null)
                    br.close();

                if (fr != null)
                    fr.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }

        }
     }
}

Some suggestions 一些建议

You can read txt file line by line using BufferedReader and related chained stream in this way: 您可以使用BufferedReader和相关链式流以这种方式逐行读取txt文件:

public class TestSO {

    public static void main(String[] args) {


        String line = "";
        try(BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("yourfile.txt")))){
                while ((line = br.readLine()) != null) {

                // Process the line

                }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

In order to split your line into pieces you need you must use line.split(":") You get 2 tokens, the one before the colon and the one after it. 为了将行分成几部分,您必须使用line.split(“:”)获得2个标记,一个标记在冒号之前,另一个在冒号之后。

String tokens[] = line.split(":");

According to JTable documentation you can create a JTable as JTable(Object[][] rowData, Object[] columnNames) 根据JTable文档,您可以将JTable创建为JTable(Object [] [] rowData,Object [] columnNames)。

So you can use tokens you read and parsed above to create the rowData array. 因此,您可以使用上面读取和解析的标记来创建rowData数组。

Then create the array for the column names: 然后为列名称创建数组:

//headers for the table
String[] columns = new String[] {
    "Col1", "Col2"
};

At the end just creare the JTable passing it the arrays with data and column names: 最后,创建JTable,将包含数据和列名称的数组传递给JTable:

//create table with data
JTable table = new JTable(rowData, columnNames);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM