簡體   English   中英

如何使用緩沖讀取器在文件中搜索字符串

[英]How do i search for a string in file using buffered reader

如何使程序在文件中搜索字符串,然后打印出該行? 我想做的是創建通訊錄類型的東西。 很抱歉,我的代碼太亂了,這是我第一次這樣做。 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),然后將File讀取到DefaultTableModel在JTable中使用內建的RowFilter (此處多次詢問和回答了這一點的所有部分),然后可以進行搜索連續從兩個或多個JTextField中獲取(此外,還需要一點點努力)

暫無
暫無

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

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