簡體   English   中英

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

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

好。 因此,我正在開發一個程序,該程序獲取已在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的數組中。 然后,您forfor循環中分別打印每個字符串:

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

aryLines[0]是您的第一個字符串。 aryLines[1]是您的第二個..依此類推,直到最后一行。 從理論上講,您可以創建一個如此處所示BufferedWriter 然后將您當前的文本文件完全復制到一個新的文件中,而這基本上只是在做您已經在做的事情。 創建BufferedWriter之后,只需使用它代替System.out.println()

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

在將每一行打印到控制台后,我不確定您要做什么。 通過將數組中的所有字符串組合為一個字符串來創建帶有兩個ss aryLiness 然后,使用整個組合的String設置TextArray

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM