有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java代码无法读取文件

当我试着运行这个程序时,它只显示“读取记录列表的开始”。但是程序没有终止,这意味着程序继续运行,但它没有输出任何内容。有人能帮忙吗

public static void readFile(String fileName, PhoneBook book_list)
{
    if(fileName == null) return;
    File file = new File(fileName);
    if(!file.isFile())
    {
        System.out.println("File not found!");
        return ;
    }

    Person person = new Person();
    boolean invalid = false;

    System.out.println("\nInfo: Beginning of Read-in records list.\n");

    try
    {
        BufferedReader input = new BufferedReader(new FileReader(file));
        try
        {
            String line = "";
            line = input.readLine();

            while(line != null)
            {
                line = line.trim();
                String[] words = line.split("\\s+");

                if(invalid)
                {
                    if(words[0].equals(""))
                    {
                        person = new Person();
                        invalid = false;
                    }
                }
                //refer to end of a record
                else if(words[0].equals(""))
                {
                    if(person.validation())
                    {
                        book_list.addPerson(person);
                        person = new Person();
                    }
                    else
                    {
                        person = new Person();
                    }
                }

                if(words[0].equalsIgnoreCase("name"))
                {
                    if(words.length<2)
                    {
                        invalid=true;
                    }
                    else
                    {
                        String name = words[1];
                        for(int i=2; i<words.length;i++)
                        {
                            name = name + " " + words[i];
                        }
                        for(int i=0; i<name.length(); i++)
                        {
                            if((name.codePointAt(i) >= 97 && name.codePointAt(i) <= 122)/*a-z*/
                                    || (name.codePointAt(i) >= 65 && name.codePointAt(i) <= 90)/*A-z*/
                                    || name.codePointAt(i) == 32)/*space*/
                            {continue;}

                            else
                            {
                                invalid=true;
                                break;
                            }
                        }
                        if(!invalid)
                        {
                            person.setName(name);
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("birthday"))
                {
                    if(words.length !=2)
                    {
                        invalid=true;
                    }

                    else
                    {
                        try
                        {
                            person.setBirthday(words[1]);
                            book_list.addPerson(person);
                        }
                        catch (ParseException e)
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("phone"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }

                    else
                    {
                        String phone = Tools.parsePhone(words[1]);
                        if(phone!=null)
                        {
                            person.setPhone(phone);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("email"))
                {
                    if(words.length != 2)
                    {
                        invalid = true;
                    }
                    else
                    {
                        if(Tools.validateEmail(words[1]))
                        {
                            person.setEmail(words[1]);
                        }
                        else
                        {
                            invalid = true;
                        }
                    }
                }

                else if(words[0].equalsIgnoreCase("address"))
                {
                    String address = line.substring(words[0].length()).trim();

                    String addr="";

                    do
                    {
                        line = input.readLine();
                        if(line == null)
                        {
                            person.setAddress(address);                             
                        }
                        if(!invalid && person.validation())
                        {
                            book_list.addPerson(person);
                        }
                        addr = line.trim();
                        String[] adds = addr.split("\\s+");

                        if(!adds[0].equals("")
                                && !adds[0].equalsIgnoreCase("name")
                                && !adds[0].equalsIgnoreCase("birthday")
                                && !adds[0].equalsIgnoreCase("phone")
                                && !adds[0].equalsIgnoreCase("email")
                                && !adds[0].equalsIgnoreCase("address"))
                        {
                            address = address + " " + addr;
                        }

                        else break;
                    }
                    while(true);

                    if(line == null)
                        break;
                }

            }// end of while loop
        }

        finally
        {
            input.close();
        }
    }
    catch(IOException ex)
    {
        System.err.print("Error: Open records file failed");
        return;
    }
    System.out.println("\nInfo: End of Read-in records list.\n");
    return;
}


public static void main(String[] args) 
{
    String personFile = null; // person contact information file
    String instFile = null; //instruction file
    String outputFile = null; //output file name;
    String reportFile = null; //report file name;



    PhoneBook book_list = new PhoneBook();
    FileIO2.readFile("C:/Users/phoenix/Desktop/sample_phonebook1.txt",  book_list);

    ArrayList<Person> a;
    a = book_list.getPersonList();

    System.out.println(a.size());


}

共 (3) 个答案

  1. # 1 楼答案

    您使用了while&;做而循环。。 而且您没有正确读取文件 在你之后。。而你应该包括line = input.readLine()

  2. # 2 楼答案

    我认为你的问题在于:

    // initializes "line" String
    String line = "";
    // Tells BufferedReader to read one line (the first line)
    line = input.readLine();
    // loops infinitely since String read from first line is not null in this specific case
    while(line != null)
    {
    

    试试这个:

    // initializes "line" String
    String line = "";
    // tells the BufferedReader to read a new line _until_ the new line is null
    // ... if new line is null we reached EOF
    while ((line = input.readLine() != null) {
    

  3. # 3 楼答案

    你应该做:

    String line = null;
    while ((line = input.readLine()) != null) {
         ...
         ...
    }
    

    而不是:

    String line = "";
    line = input.readLine();
    
    while(line != null){
         ...
         ...
    }
    

    继续读文件。干杯P