Java GUI - 读取文本文件,其中文本文件的每一行都是它自己的字符串

标签 java user-interface

好的。因此,我正在开发一个程序,该程序获取已放入 GUI 中文本字段的信息,并将该信息放入文本文件中,文件名是动物的名称和所有者的姓氏。

//该部分已完成。

然后能够使用动物名称和所有者姓氏搜索文件,并能够将信息放入单独的文本字段中。这看起来就像您首先放置信息的页面。然后就可以将更改后的信息保存到同一个文本文件中。

现在我的问题是如何从具有不同行的文本文件中获取信息,然后将每一行放入自己的字符串中。

这是程序读取文本文件的部分

`import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {

  private String path;


  public ReadFile(String file_path) {
    path = file_path;
  }

  public String[] OpenFile() throws IOException {

    FileReader fr = new FileReader (path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = 20; 
    String[] textData = new String[numberOfLines];

    int i;

    for (i=0; i < numberOfLines; i++) {
      textData[i] = textReader.readLine();

  }
    textReader.close();
    return textData;
} 
   int readLines() throws IOException {
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) != null) {
      numberOfLines++;
    }
           bf.close();

           return numberOfLines;
           }
}
`
// Here is where I am using this code

    `JButton b7 = new JButton("Done");
         b7.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
             f4.setVisible(true);
             f3.setVisible(false);
             scrollPane.revalidate();
             scrollPane.repaint();
                  String namess = names.getText();
                 na.setText(namess);
                 String ownerslss = ownersls.getText();
                 ol.setText(ownerslss); 
                   String file_name =namess + " " + ownerslss + ".txt";
                                    na.setText(namess);

                 ol.setText(ownerslss); 
                 try { 
                   ReadFile file = new ReadFile (file_name);
                   String [] aryLines = file.OpenFile();
                    String aryLiness ="";
                   int item, space;
                   for (item=0; item < aryLines.length; item++) {
                     System.out.println(aryLines[item]);
                     aryLiness = Arrays.toString(aryLines);

                      space = aryLines.length;
                   }
                    space = aryLines.length;
    //< assname is a textarray that is the only way I could get the words to go down the page but it doesn't look good at all. Because it doesn't skip lines...
                   assname.setSize(20 ,space);
                   assname.append("" + aryLiness);
                     panimals.add(assname);  
                 }

                 catch (IOException wewe) {
                   System.out.println(wewe.getMessage() );

      }

     }
         });`

最佳答案

了解数组的工作原理非常重要,因为它们的使用非常频繁。

String[] myStringArray = new String[3];

该数组包含 3 个 String 对象。它本质上与您所做的相同:

String oneString = null;  //same as myStringArray[0]
String twoString = null;  //same as myStringArray[1]
String threeString = null;//same as myStringArray[2]

这意味着,在将所有行读入Strings后,您可以像这样创建JTextField:

JTextField myTextField1 = new JTextField(myStringArray[0]);
JTextField myTextField2 = new JTextField(myStringArray[1]);
JTextField myTextField3 = new JTextField(myStringArray[2]);

或者,一次分配一个数组一行:

JTextField[] myTextFields = new JTextField[3];
myTextFields[0] = new JTextField(myStringArray[0]);
myTextFields[1] = new JTextField(myStringArray[1]);
myTextFields[2] = new JTextField(myStringArray[2]);

现在,想象一下,如果您有 300 个 StringsTextFields,您绝对不会想将这些行键入 300 次。这就是 for 循环很棒的原因。

JTextField[] myTextFields = new JTextField[300];
for (int i = 0; i < myTextFields.length; i++)
{
    myTextFields[i] = new JTextField(myStringArray[i]);
}
<小时/>

不确定您是否意识到,但您当前已将文本文件中的每一行读入一个名为 aryLines 的数组中。然后,您可以在 for 循环中单独打印每个字符串:

System.out.println(aryLines[item]); //item is starting at 0, goes through every String

aryLines[0] 是您的第一个字符串。 aryLines[1] 是你的第二行......依此类推,直到最后一行。理论上,您可以创建一个 BufferedWriter like shown here并将当前的文本文件完全复制到一个新的文本文件中,基本上只做您已经在做的事情。创建 BufferedWriter 后,您只需使用它来代替 System.out.println() 即可:

writer.write(aryLines[item]);
writer.newLine();
<小时/>

将每一行打印到控制台后,我不确定您要做什么。具有两个 ssaryLiness 是通过将数组中的所有字符串组合成一个字符串而创建的。然后用整个组合字符串设置 TextArray

关于Java GUI - 读取文本文件,其中文本文件的每一行都是它自己的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31254340/

相关文章:

java - 如何用java制作 "background"桌面程序?

java - 使用 Java 是开发基于 GUI 的会计应用程序的合适语言/平台吗?

java - Android 中的 View 占据整个屏幕 - 如何告诉 Canvas 尊重 View 的限制?

objective-c - GCC 有 GUI 吗?

.net - 查找GUI控件的所有者线程

java - 在 spring MVC 中从日期验证时获取不同格式的日期

user-interface - 在加工草图中,我可以控制显示窗口的位置吗?

java - 哪些部分或包可以从 JDK 中删除?

java - 正确分发 Java 桌面应用程序

java - 如何在jsp/servlet中设置密码到期日期?