简体   繁体   English

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

[英]Java GUI - reading textfiles having each line of the text file as it's own string

Ok. 好。 So I am working on a program that gets information that has been put into textfields in a gui and puts that information into a text file and the name of the file is the name of the animal and name of the owners last name. 因此,我正在开发一个程序,该程序获取已在gui中放入文本字​​段的信息,并将该信息放入文本文件中,并且文件名是动物的名字和所有者的姓氏。

//that part is done. //这部分完成了。

Then able to search for the file using the name of the animal and the owners last name, and being able to have the information be put into separate text fields. 然后可以使用动物的名字和所有者的姓氏来搜索文件,并且能够将信息放入单独的文本字段中。 That look like the page where you first put down the information. 看起来就像是您第一次放置信息的页面。 Then being able to save the information changed to the same text file. 然后便能够将信息保存到相同的文本文件中。

Now my question is how do I get the information from a text file that has different lines and then put each line in its own String. 现在我的问题是如何从具有不同行的文本文件中获取信息,然后将每行放入其自己的字符串中。

Here is the part of the program that reads the text file 这是程序中读取文本文件的部分

`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() );

      }

     }
         });`

It's important to know how arrays work, because they are used very often. 了解数组的工作原理非常重要,因为它们经常使用。

String[] myStringArray = new String[3];

This array holds 3 String objects. 该数组包含3个String对象。 It is essentially the same as if you had done: 它基本上与您完成的操作相同:

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

This means, after you read all your lines into Strings , you can create your JTextField like so: 这意味着,在将所有行读入Strings ,可以像下面这样创建JTextField

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

or, assigning an array one line at a time: 或者,一次将一个数组分配给一行:

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

Now, imagine if you had 300 Strings and TextFields , there's no way you would want to type those lines 300 times. 现在,想象一下,如果您有300个StringsTextFields ,那么您根本不想将这些行键入300次。 That's why for loops are awesome. 这就是为什么for循环很棒。

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

Not sure if you realize, but you currently read every line in your textfile into an array already, named aryLines . 不知道是否意识到,但是当前您已将文本文件中的每一行读入一个名为aryLines的数组中。 You then print each String individually in your for loop: 然后,您forfor循环中分别打印每个字符串:

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

aryLines[0] is your first string. aryLines[0]是您的第一个字符串。 aryLines[1] is your second.. and so on until the last line. aryLines[1]是您的第二个..依此类推,直到最后一行。 Theoretically, you could create a BufferedWriter like shown here and completely copy your current textfile into a new one just doing basically what you are already doing. 从理论上讲,您可以创建一个如此处所示BufferedWriter 然后将您当前的文本文件完全复制到一个新的文件中,而这基本上只是在做您已经在做的事情。 You would just use this instead of the System.out.println() after creating your BufferedWriter: 创建BufferedWriter之后,只需使用它代替System.out.println()

writer.write(aryLines[item]);
writer.newLine();

After you print out every line to the console, I'm not sure what you mean to do. 在将每一行打印到控制台后,我不确定您要做什么。 aryLiness with two ss is created by combining all the Strings in your array into one String. 通过将数组中的所有字符串组合为一个字符串来创建带有两个ss aryLiness Then you set your TextArray with the entire combined String. 然后,使用整个组合的String设置TextArray

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

相关问题 Java读取文本文件并将每一行另存为新的字符串数组 - Java Reading Text File And Saving Each Line As a New String Array Java如何读取txt文件并使每一行成为自己的字符串数组 - Java how to read a txt file and make each line it's own string array 阅读巨大的文本文件Java - Reading huge textfiles Java Swing Java GUI使用扫描仪读取文本文件 - Swing Java GUI reading a text file with scanner Java读取文本文件并将每一行存储到自己的数组中 - Java reading textfile and store each line to its own array 从字符串写入java文件(通过逐行读取文本文件中的字符串内容) - write a java file from a string (The string content from a text file by reading it line by line) 从文本文件中逐行读取有理数,并在每行中添加数字,其中某些行输入错误 - Reading rational numbers from a text file line by line and add numbers in each line with some lines having wrong input 在 Java 中逐行读取文本文件 - Reading from text file line by line in Java Java读取多行文本文件,在每行末尾最后一个字符后添加分隔符 - Java reading multi line text file, add delimiter at the end of each line after final character 读取每行包含特定信息的文件时遇到麻烦 - Having a bit of trouble reading in a file that contains specific information in each line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM