gpt4 book ai didi

java - 将数据从一个非常大的文本文件导入到 JTable

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:05:37 25 4
gpt4 key购买 nike

我正在制作一个处理存储在文本文件中的大量数据的应用程序。本质上,应用程序浏览一个.txt文件,一旦找到,应用程序需要将文件中的所有数据放入一个JTable中,然后我需要对数据进行一些过滤操作,然后将其导出。.txt 文件中的数据采用以下格式:

Column1 Column2 Column3 Column4 Column5 Column6 .........(there are 510 columns)
A B C D E F
G H I J K L
.... ... ... ... .... ...

有数千行。每行由双类型数字组成(A、B....均为 1.3、2.0 等)

我通过手动添加数组中的所有列名然后将表的模型设置为来设置表的列

table = new JTable();
table.setModel(new DefaultTableModel(null,columns));
//'columns' is a string array containing 510 names

我在这里将行设置为“空”,因为我不知道如何获取该数据并将其转换为二维数组,因为这是行的格式。此外,可能有任意数量的行。我如何读取文本文件的代码如下:

    btnBrowse.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent arg0)
{
JFileChooser chooser = new JFileChooser();

int option = chooser.showOpenDialog(dpaGUI.this);
if (option == JFileChooser.APPROVE_OPTION)
{
file = chooser.getSelectedFile();
BufferedReader in = null;
try
{
in = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}

try
{
while(in.ready())
{
//test operations performed with the retrieved text
}
}
catch (IOException e)
{

e.printStackTrace();
}
try
{
in.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});

我尝试执行一些测试操作,例如用“\t”拆分检索到的文本(这就是将其存储在文本文件中的方式),但到目前为止没有任何帮助。文本文件还包含列的名称,因此在填充行时需要删除它们。关于如何将大量数据从我的文本文件获取到 JTable 中的任何建议都会有所帮助。我正在使用 JTable,因为我需要对整个数据列(每个列都有数千行)执行进一步的操作。我愿意接受关于不同容器的建议。任何建议都会有所帮助。多谢!

最佳答案

试一试。您可以使用接受列名和行数的构造函数,而不是将数据设置为 null。将行设置为 0,只需使用 addRow

DefaultTableModel(Object[] cols, int rows)

DefaultTableModel 查看更多方法和构造函数应用程序接口(interface)。

使用您的特定代码,您可以使用我的方法创建一个 DefaultTableModel 并在您的 ActionListener 中使用 table.setModel(model)

文件

Column1 Column2 Column3 Column4 Column5 Column6 
A B C D E F
G H I J K L
A B C D E F
G H I J K L
A B C D E F
G H I J K L
A B C D E F
G H I J K L
A B C D E F
G H I J K L
A B C D E F
G H I J K L

结果

enter image description here

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTable5 {

public TestTable5() {
DefaultTableModel model = createModel("/resources/file.txt");
JTable table = new JTable(model);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private DefaultTableModel createModel(String filePath) {
DefaultTableModel model = null;

try {
BufferedReader txtReader = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(filePath)));
String header = txtReader.readLine();
model = new DefaultTableModel(header.split("\\s+"), 0);
String line;
while((line = txtReader.readLine()) != null) {
model.addRow(line.split("\\s+"));
}
} catch (IOException ex) {
ex.printStackTrace();
}

return model;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestTable5();
}
});
}
}

更新

您可以尝试使用 JFileChooser

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TestTable5 {

public TestTable5() {
String[] defaultCols = {"COL", "COL", "COL", "COL", "COL", "COL"};
DefaultTableModel model = new DefaultTableModel(defaultCols, 0);
JTable table = new JTable(model);

JButton button = createButton(table);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(table));
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private JButton createButton(final JTable table) {
JButton button = new JButton("Get File");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
int result = chooser.showOpenDialog(table);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
DefaultTableModel model = createModel(file);
table.setModel(model);
}
}
});
return button;
}

private DefaultTableModel createModel(File file) {
DefaultTableModel model = null;

try {
BufferedReader txtReader = new BufferedReader(
new FileReader(file));
String header = txtReader.readLine();
model = new DefaultTableModel(header.split("\\s+"), 0);
String line;
while ((line = txtReader.readLine()) != null) {
model.addRow(line.split("\\s+"));
}
} catch (IOException ex) {
ex.printStackTrace();
}

return model;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestTable5();
}
});
}
}

关于java - 将数据从一个非常大的文本文件导入到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com