简体   繁体   English

有什么方法可以读取.txt文件并在Java中制作内容的二维数组?

[英]Is there any way to read a .txt file and make a 2d array of the contents in Java?

I want a JTable to be populated with certain information. 我希望在JTable中填充某些信息。 That information (ie the values stored in rows of the table) changes periodically. 该信息(即存储在表行中的值)会定期更改。 What I thought to implement it was storing the contents everytime the JTable is updated in a .txt file and then display that text file in a JTable. 我想实现的目的是每次JTable更新时将内容存储在.txt文件中,然后在JTable中显示该文本文件。

Now my question is that is it a good approach? 现在我的问题是这是一个好方法吗? I mean if I store the contents of a text file into a JTable, when something is updated in the JTable, that updation will not be shown to the user unless they re-run the app (because a text file can't be read and written all at once). 我的意思是,如果我将文本文件的内容存储到JTable中,则当JTable中的某些内容更新时,该更新将不会显示给用户,除非他们重新运行该应用程序(因为无法读取文本文件,并且一次写完)。

Can anyone suggest me a good approach to it? 谁能建议我一个好的方法? The app needs to be real time so every time ie I want the users to see the updated information dynamically while the app is running. 该应用程序必须是实时的,因此每次都希望用户在应用程序运行时动态查看更新的信息。 I dont want them to close the app again and again to see the updated inofrmation. 我不希望他们一次又一次关闭应用程序以查看更新的信息。 I'm using Java and linux. 我正在使用Java和Linux。

Here's another question on stack overflow that may help you out 这是关于堆栈溢出的另一个问题,可能会帮助您

Reading contents of a file into a 2D array 将文件内容读入2D数组

here's the code that reads a file into a 2D array 这是将文件读入2D数组的代码

FileInputStream inputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(inputStream);
DataInputStream in = new DataInputStream(inputStream);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));

int lineCount = 0;
while ((line = bf.readLine()) != null)
{
    String[] numbers = line.split(" ");
    for ( int i = 0 ; i < 3 ; i++) 
         matrix[lineCount][i] = Double.parseDouble(numbers[i]);

    lineCount++;
}

bf.close();

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

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