Java 代码无法读取文件
当我试着运行这个程序时,它只显示“读取记录列表的开始”。但是程序没有终止,这意味着程序继续运行,但它没有输出任何内容。有人能帮忙吗Java 代码无法读取文件,java,file-io,io,bufferedreader,Java,File Io,Io,Bufferedreader,当我试着运行这个程序时,它只显示“读取记录列表的开始”。但是程序没有终止,这意味着程序继续运行,但它没有输出任何内容。有人能帮忙吗 public static void readFile(String fileName, PhoneBook book_list) { if(fileName == null) return; File file = new File(fileName); if(!file.isFile()) { System.out
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());
}
publicstaticvoidreadfile(字符串文件名、电话簿列表)
{
如果(fileName==null)返回;
文件=新文件(文件名);
如果(!file.isFile())
{
System.out.println(“未找到文件!”);
回来
}
Person=新人();
布尔无效=假;
System.out.println(“\n信息:读入记录列表的开始。\n”);
尝试
{
BufferedReader输入=新BufferedReader(新文件读取器(文件));
尝试
{
字符串行=”;
line=input.readLine();
while(行!=null)
{
line=line.trim();
String[]words=line.split(\\s+);
如果(无效)
{
if(字[0]。等于(“”)
{
person=新的person();
无效=错误;
}
}
//指记录的结尾
else if(字[0]。等于(“”)
{
if(person.validation())
{
账簿清单。添加人(人);
person=新的person();
}
其他的
{
person=新的person();
}
}
if(单词[0].equalsIgnoreCase(“名称”))
{
如果(words.length我想你的问题在这里:
// 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) {
…您已使用while&do..while循环。。
并且您没有正确读取文件
在do.之后,当您应该包括line=input.readLine()
时,您应该:
String line = null;
while ((line = input.readLine()) != null) {
...
...
}
而不是:
String line = "";
line = input.readLine();
while(line != null){
...
...
}
继续阅读文件。干杯!:p你使用调试器查看发生了什么吗?嗯,这是一个200行长的静态方法吗?我认为你应该重新设计和重构你的代码。如果你做得好,你可以很快发现问题。可以帮你很多。顺便说一句,你只是在循环前阅读一行,遇到地址时才阅读(因为你没有读到那么远,所以你不会这么做。)天哪,天哪..你怎么能这么快就想出来~~你能告诉我到底有什么区别吗?我想它们是一样的。好吧:)现在编辑和评论我的答案…非常感谢。我怎么能给你奖励或奖励呢?因为我今天刚刚使用了这个堆栈溢出。”告诉BufferedReader阅读新行,直到新行不为空”-实际上,直到新行为空,或者换句话说,新行不为空。:)你的语法巨魔XD-重新编辑我的帖子以符合正确的英语