Java 如何显示文件中的特定数据

Java 如何显示文件中的特定数据,java,bufferedreader,Java,Bufferedreader,我的程序应该要求用户输入名字、姓氏和电话号码,直到用户停止。然后,当显示时,它会要求输入名字,并在文本文件中搜索以查找所有同名信息,并显示匹配的姓氏和电话 import java.util.*; import java.io.*; import java.util.Scanner; public class WritePhoneList { public static void main(String[] args)throws IOException { BufferedWriter

我的程序应该要求用户输入名字、姓氏和电话号码,直到用户停止。然后,当显示时,它会要求输入名字,并在文本文件中搜索以查找所有同名信息,并显示匹配的姓氏和电话

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class WritePhoneList
{
public static void main(String[] args)throws IOException
{


  BufferedWriter output = new BufferedWriter(new FileWriter(new File(
                                        "PhoneFile.txt"), true));

String name, lname, age;
int pos,choice;

try
{

do
{
Scanner input = new Scanner(System.in);
System.out.print("Enter First name, last name, and phone number ");
name = input.nextLine();


output.write(name);
output.newLine();

System.out.print("Would you like to add another? yes(1)/no(2)");
choice = input.nextInt();
}while(choice == 1);
output.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
这是显示代码,当我搜索姓名时,它会找到匹配项,但会显示相同姓名的姓氏和电话号码3次,我希望它显示所有可能与姓氏匹配的项

import java.util.*;
import java.io.*;
import java.util.Scanner;
public class DisplaySelectedNumbers
{
public static void main(String[] args)throws IOException
{

String name;
String strLine;
try
{
FileInputStream fstream = new FileInputStream("PhoneFile.txt");
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
          Scanner input = new Scanner(System.in);
          System.out.print("Enter a first name");
          name = input.nextLine();

        strLine= br.readLine();
        String[] line = strLine.split(" ");
        String part1 = line[0]; 
        String part2 = line[1];
        String part3 = line[2];

        //Read File Line By Line
        while ((strLine= br.readLine()) != null)    
    {

        if(name.equals(part1)) 
        {

        // Print the content on the console
System.out.print("\n" + part2 + " " + part3);
}   
}
}catch (Exception e)
{//Catch exception if any
            System.out.println("Error: " + e.getMessage());
}

}
}

您需要分割线并在while循环中设置部件:

    FileInputStream fstream = new FileInputStream("PhoneFile.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a first name");
    name = input.nextLine();

    String[] line;
    String part1, part2, part3;

    //Read File Line By Line
    while ((strLine= br.readLine()) != null)    
    {
        line = strLine.split(" ");
        part1 = line[0]; 
        part2 = line[1];
        part3 = line[2];

        if(name.equals(part1)) 
        {
            System.out.print("\n" + part2 + " " + part3);
        }


    }

我犯了个愚蠢的错误。。。我用正确的代码编辑了我的答案