使用JTextfield按关键字搜索Txt文件中的字符串,并打印出整个字符串

时间:2016-11-23 14:18:19

标签: java swing search arraylist jtextfield

我在数组中添加了一些文本并将其存储在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);
        }
    }
}

0 个答案:

没有答案
相关问题