Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing file to java issue

Tags:

java

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();
            }
like image 664
adit Avatar asked Nov 21 '25 16:11

adit


1 Answers

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

like image 50
Kaleb Brasee Avatar answered Nov 24 '25 06:11

Kaleb Brasee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!