删除文本文件中的用户指定文本

时间:2017-05-19 13:36:09

标签: java arrays text-files

我正在为一个涉及从文本文件中删除一个特定条目的类进行分配。这是具体问题

  

创建一个新程序,该程序将访问您的数据文件'phone_directory.txt'和   允许用户删除数据文件中的一个名称和电话号码。至   为此,您需要先将数据文件读入数组(务必计算数据   创建正确大小的数组的记录数)。对数据文件进行排序   升序。现在,当用户输入要删除的名称时,您可以进行搜索   您的数据文件并显示符合输入名称的记录的信息

所以我已经有了一个允许用户输入其他名称的先前文件,代码在这里:

public static void main (String[] args) throws IOException
{
    c = new Console ();

    int count = 0;

    String[] FirstNames = new String [10];
    String[] LastNames = new String [10];
    String[] PhoneNumber = new String [10];

    BufferedReader input;
    input = new BufferedReader (new FileReader ("phone_directory.txt"));

    for (int i = 0 ; i < 10 ; i++)
    {
        FirstNames [i] = input.readLine ();
        LastNames [i] = input.readLine ();
        PhoneNumber [i] = input.readLine ();
        //count++;
    }

    input.close ();
    PrintWriter output;
    output = new PrintWriter (new FileWriter ("phone_directory.txt"));

    for (int k = 0 ; k < 10 ; k++)
    {
        output.println (FirstNames [k]);

        output.println (LastNames [k]);

        output.println (PhoneNumber [k]);
    }
    c.println ("Please enter the first name or end to exit: ");
    String FirstName = c.readLine ();
    while (!FirstName.equals("end"))
    {
        output.println (FirstName);

        c.println ("Please enter the last name: ");
        String LastName = c.readLine ();
        output.println (LastName);

        c.println ("Please enter the phone number of this person (no area code): ");
        String PhoneNumbers = c.readLine ();
        output.println (PhoneNumbers);
        c.println ("Please enter the first name: ");
        FirstName = c.readLine ();
    }

    output.close ();

然而,当涉及删除条目时,我不知道从哪里开始。我假设我必须将它们输入到类似于上一个问题的数组中,然后向用户询问名称,如果它们是匹配项,则以某种方式从文本文件中删除这些名称。我正在使用&#34;准备编程&#34;而且语言是Java。

0 个答案:

没有答案