读文件,读出的内容与原数据不一致

zhang_m_m 2016-12-18 10:08:33
void number() {

File f1 = new File("e:\\work");
f1.mkdirs();
File f2 = new File("e:\\work\\sanguozhi.txt");
try {
f2.createNewFile();
} catch (IOException e3) {
e3.printStackTrace();
}

ArrayList<work.Student> al = new ArrayList<>();
al.add(new work.Student("曹操", 97));
al.add(new work.Student("刘备", 93));
al.add(new work.Student("孙权", 90));
al.add(new work.Student("吕布", 80));
al.add(new work.Student("关羽", 85));

PrintWriter pw = null;
try {
pw = new PrintWriter(f2);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}

for (work.Student student : al) {
pw.println(student.toString());
pw.flush();

}
pw.close();

ArrayList<work.Student> al1 = new ArrayList<>();
FileReader fr = null;
try {

fr = new FileReader(f2);
char[] ah = new char[1000];
int i=fr.read(ah);

i=fr.read(ah);
String student =new String(ah);
//System.out.println(student);
String[] student1 = student.split("\\n");
System.out.println(student1.length);
for (int j=0;j<(student1.length)-1;j++) {
System.out.println(student1[j]);
// String[] student2 = student1[j].split(",");
// work.Student stu = new work.Student();
// stu.setName(student2[0]);
// System.out.println(student2[0]+student2[1]+"1");
// stu.setAge(Integer.parseInt((student2[1].trim())));
// al1.add(stu);
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

for (work.Student string : al1) {
System.out.println(string);
}

}


哪位大神帮忙看下这段代码,将数据写入文件,然后读出来,换行符分割下,就出现很多莫名其秒的空行 。。
...全文
243 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Be_nurturing 2016-12-19
  • 打赏
  • 举报
回复
这代码看着好累。
嘻哈愚者 2016-12-18
  • 打赏
  • 举报
回复
你类都不全, 结果也不贴。 这种情况有几个方面: 1.写入方法. 2.toString()方法。 3.输出方法。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧