当我试图运行这个程序时,它只显示“读入记录列表的开头。但程序没有终止,这意味着程序继续运行,但它没有输出任何东西。有人可以帮忙吗?
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());
}
答案 0 :(得分:3)
我认为你的问题在这里:
// 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) {
...
答案 1 :(得分:1)
你应该这样做:
String line = null;
while ((line = input.readLine()) != null) {
...
...
}
而不是:
String line = "";
line = input.readLine();
while(line != null){
...
...
}
继续阅读文件。干杯! :P
答案 2 :(得分:0)
你曾经使用过&amp; amp; do..while循环..
而且你没有正确阅读文件
之后你应该包括line = input.readLine()