java - 如何使用缓冲阅读器在文件中搜索字符串

标签 java swing

如何让程序在文件中搜索字符串,然后打印出该行? 我想做的是创建一个地址簿类型的东西。如果我的代码很困惑,我很抱歉,因为这是我第一次这样做。 HashMap 对于这种事情会更好吗?如果是这样,是否可以将多个值附加到同一个键上?

public class contats extends JFrame {
    JLabel nameLabel;
    JLabel phoneLabel;
    JLabel notesLabel;
    JLabel searchLabel;
    JTextField name;
    JTextField phone;
    JTextField notes;
    JTextField searchField;
    JButton add;
    JButton search;

        public contats() {
        setLayout(new FlowLayout());

        nameLabel = new JLabel("Name: ");
        add(nameLabel);

        name = new JTextField(15);
        add(name);

        phoneLabel = new JLabel("Number: ");
        add(phoneLabel);

        phone = new JTextField(15);
        add(phone);

        notesLabel = new JLabel("Notes: ");
        add(notesLabel);

        notes = new JTextField(10);
        add(notes);

        add = new JButton("Add Contact");
        add(add);

        searchLabel = new JLabel("search");
        add(searchLabel);

        searchField = new JTextField(15);
        add(searchField);

        search = new JButton("Search");
        add(search);

        event e = new event();
        add.addActionListener(e);
        search.addActionListener(e);


    }

       public class event implements ActionListener{
        public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){
            try {
                String[] con = {name.getText(),phone.getText(),notes.getText()};
                BufferedWriter bw = new BufferedWriter(new FileWriter("peewee.txt", true));
                for(String s : con){
                    bw.newLine();
                    bw.write(s);
                }
                bw.close();

            }catch(Exception ex){
                JOptionPane.showMessageDialog(null,  "blerr");
            }

        } else if(e.getSource() == search){
            try{
                String input = search.getText();
                String file;
                String searchArray[];

                BufferedReader br = new BufferedReader(new FileReader ("peewee.txt"));
                while((file = br.readLine())!= null){ // this is where i need the help??
                    if(file == input){
                    System.out.println(input);  
                    }
                }
                br.close();

            } catch(Exception ex) {
                JOptionPane.showMessageDialog(null, "sdfadsfsdf");

            }
        }
    }

}

    public static void main(String[] args) {
        contats gui = new contats();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 300);
        gui.setTitle("Cobtacts");
        gui.setVisible(true);



    }
}

最佳答案

使用

更好、更好、更快,可能是一个想法

  • 您的描述,发布的代码谈论多个搜索字段(JTextField),然后将文件读取到 DefaultTableModel并使用内置 RowFilter in JTable ,(这一点的所有部分都在这里被多次询问和回答),那么就可以从两个或多个 JTextField 连续搜索(而且还需要一点点努力)

关于java - 如何使用缓冲阅读器在文件中搜索字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473112/

相关文章:

java - 关闭打开的 hibernate session (如果存在)

java - 用于制作代码编辑器的 JSyntaxPane 的 JavaFX 等价物是什么?

java - 如何获取地理位置详细信息位置错误原因

java - 如何调整JFrame和JInternal框架与任何屏幕尺寸?

java - 如何防止我的 JScrollPane/JTextArea 超出包含的 JTabbedPane?

java - 创建基于 GUI 的计时器(或秒表)

java - Spring RestTemplate 发布响应

Java泛型类参数类型推断

java - 更改 String.xml 的值

java - 使用 xuggler 从图片创建 mp4 和 java mp3