我有一个使用BufferedReader写入文件的java程序,但是,在文件的末尾它总是因为它没有完成而结束。如下所示:
41,21,true,30,2010-08-07,Answer
2239,1163,true,10,2010-10-06,Answer
641,364,true,10,2010-09-02,Answer
715,149,true,5,2010-08-11,Comment
219,177,true,25,2010-07-23,Answer
4809,4591,true,10,2011-02-11,Answer
4086,4853,true,10,2011-02-17,Answer
3720,2611,true,10,2010-12-08,Answer
5101,60,true,5,2011-03-07,Comment
1393,689,true,20,2010-08-24,Answer
1274,450,true,5,2010-08-09,Comment
446,4853,true,5,2011-03-07,Comment
2191,70,true,5,2011-02-05,Comment
1487,1281,true,10,2010-08-05,Answer
3479,3899,true,10,2010-12-25,Answer
1393,685,true,10,2010-07-26,Answer
364,649,true,5,2010-07-19,Comment
1274,1236,true,10,2010-08-19,Answer
99,18
怎么会发生这种情况?我的大多数程序都在循环中执行它。这是其中的一部分:
try {
output.write("nodedef>name VARCHAR,label VARCHAR,age INT,reputation INT,location VARCHAR(100),upvote INT,downvote INT,creationDate VARCHAR\n");
Iterator<Integer> itr = set.iterator();
while(itr.hasNext()){
Users temp = users.get(Integer.parseInt(itr.next().toString()));
if (temp != null)
output.write(temp.id + ", " + temp.displayName + "," + temp.age + "," + temp.reputation + "," + temp.location + "," + temp.upVotes + "," + temp.downVotes + "," + temp.date + "\n" );
}
output.write("edgedef>node1 VARCHAR,node2 VARCHAR,directed BOOLEAN, weight DOUBLE, creationDate VARCHAR, postType VARCHAR\n");
Enumeration<Edges> h = edges.elements();
while (h.hasMoreElements()){
Edges edge = h.nextElement();
output.write(edge.sourceUserId + "," + edge.targetUserId + ",true," + edge.weight + "," + edge.creationDate + "," + edge.type + "\n");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
答案 0 :(得分:6)
确保在写完后,在OutputStream上调用flush()
。