3

I have a java program that writes a file using the BufferedReader, however, at the end of the file it always ends up as it was not finished. Something like as follows:

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

How could this have happened? Most of my programs are executing it inside a loop. Here's part of it:

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();
            }
1
  • 1
    Did it ever get an IOException? It looks like you just stubbed the exception handler. Commented Apr 23, 2011 at 22:56

1 Answer 1

6

Make sure to call flush() on the OutputStream when you're done writing to it.

Sign up to request clarification or add additional context in comments.

1 Comment

close calls flush, so you don't need flush, if you call close.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.