我在数组中添加了一些文本并将其存储在txt文件中。现在我需要使用JTextfield
通过ID搜索txt文件,并将相应的字符串项从文件输出到JFrame
。
public class search implements ActionListener
{
public void actionPerformed(ActionEvent evtSeacrh)
{
String filename = "CustomerList.txt";
String cusid = txtCusId.getText();
String cusFName = txtFName.getText();
List<String> aList = new ArrayList<String>();
//Read the lines of text into an ArrayList
try
{
BufferedReader input = new BufferedReader(new FileReader(filename));
if(!input.ready()) { //check whether the file can be read
throw new IOException();
}
String line = null;
while ((line = input.readLine()) !=null) { //Read a line of Text
if (line.contains(cusid)) {
aList.add(line);
}
}
input.close();
} catch (IOException e) {
System.out.println(e);
}
}
}