Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferedReader and FileChannel  RSS feed

 
L Bunny
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code (BufferedReader) to write data that I read from a table to an output file.

8 records are retrieved from a table and it tooks 00:00:01.40300 to write to an output file and the output file size is 3KB.

Then, i use FileChannel to do the same thing. But it tooks 00:00:01.63200 to write to an output file and the outfile file size is 2151KB

Question:-
1. There is no way for me to pre-allocate ByteBuffer size because I won't know the size until i read the value of a column from ResultSet, right?

2. Should I make a conclusion that BufferedWriter is better than FileChannel when reading data from a table and writing it to an output file? If I am wrong, can someone please help to correct my mistake by providing some source code to modify my FileChannel source code?

Thanks to all the guru and experts 1st.
[ November 27, 2007: Message edited by: Jim Yingst ]
 
L Bunny
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction:
It should be BufferedWriter and FileChannel
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your post above for readability. I recommend you use them in the future.

[LB]: 1. There is no way for me to pre-allocate ByteBuffer size because I won't know the size until i read the value of a column from ResultSet, right?

Well, it's possible to reuse the same ByteBuffer. Even if it's too small to transfer all the bytes of a single record, you can use it to make the transfer by breaking the record up into smaller parts. I'm not sure it's worth the time though. I don't think you're doing anything here that's faster with NIO, and more importantly there are probably other bugs to fix first.

[LB]: 2. Should I make a conclusion that BufferedWriter is better than FileChannel when reading data from a table and writing it to an output file? If I am wrong, can someone please help to correct my mistake by providing some source code to modify my FileChannel source code?

That conclusion is far too general to make here. Since the file sizes are so different, it's obvious that at least one of these methods is badly wrong. Have you looked at the output file? Can you tell whether either one looks like it should? If one is 3 KB and the other is 2151 KB, the files must be very different. Can you tell anything about them by looking at the files? That could give important clues as to which method has bugs in it. (Of course, it's possible that they both have bugs...)

In the ByteBuffer code, I see you creating many new ByteBuffers, and putting data in them. But only the last one is being written, it seems. That doesn't seem right.

Another difference is that in the first code, you're creating a FileWriter that overwrites any existing file. In the new code, you're creating a FileOutputStream that appends to an existing file. Which behavior is correct? That could well explain why the later file is so big.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!