简体   繁体   English

Java搜索文本文件

[英]Java search a text file

I have made a phone bill system takes input of phone number called, date of the call and call length. 我已经制作了一个电话账单系统,输入了所谓的电话号码,通话日期和通话时长。 it the saves it in to a text file. 它将它保存到文本文件中。 what i have not been able to do is search the text file for the phone number. 我无法做的是在文本文件中搜索电话号码。

My coding 我的编码

just do linear search (iterating over your phone list) : 只做线性搜索(迭代你的手机列表):

public static List<Phone> searchPhone(final String phoneNumber, final List<Phone> phoneList) {
    List<Phone> matchedPhone = new ArrayList<Phone>();

    for(Phone phone: phoneList) {
        if ( phone.getphoneNumber().equals(phoneNumber) ) { 
            matchedPhone.add(phone);
        }
    }

    return matchedPhone;
}

also for readability, don't make your method parameter as output. 同样为了便于阅读,请不要将您的方法参数作为输出。 its not good practice, so you should change your method from: 这不是一个好习惯,所以你应该改变你的方法:

static void readList(List<Phone> phoneListIn) {
}

to: 至:

   static List<Phone> readList(final String fileName) {
   }

output arguments should be avoided as possible as you can 尽可能避免输出参数

Here is a link to some code that I found using Google. 这是我使用Google找到的一些代码的链接。

http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/ http://www.dreamincode.net/forums/topic/48905-search-inside-a-text-file/

Perform Collections.sort(List list); 执行Collections.sort(List list);

Then perform Collections.binarySearch(List list, Object key). 然后执行Collections.binarySearch(List list, Object key). This should achieve the searching efficiently. 这应该有效地实现搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM