• Post Reply Bookmark Topic Watch Topic
  • New Topic

DeflaterOutputStream not writing anything to text file after compression

 
Anka Rao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please suggest where my code is wrong..

It is printing all the content of input file (is having around 100 lines) with System.out.print ((char)i); But destination file is having data as after running code for compression. Surprisingly, eclipse compiler is not giving any errors..



Console output :

Compression of following data has started..
Compression is completed..
 
Maxim Karvonen
Ranch Hand
Posts: 121
12
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Anka

My suggestion is that your are closing file output stream before deflater output stream. Deflater have an internal buffer and process data in "batches" by compressing them. It ocasionally write data and also finishes compression and flushes the buffer when close() method is called. However, file stream is closed one line above so all the remaining data are lost and the resulting file is "truncated". Switching an order should help. Or even better, you do not have to close the file stream by hand. DeflaterOutputStream will close the underlying stream when dos.close() is called.

BTW, do you get any exceptions running your program? An exception should happen in line 17, where DeflaterOutputStream tries to write into the already-closed file stream. So the code should say "Exception in Compress/Decompress program".
 
Anka Rao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maxim.. It worked .. just closed DeflaterOutputStream before FileOutputStream..
 
Tony Docherty
Saloon Keeper
Posts: 3142
72
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or better still do as Maxim suggested and don't close the FileOutputStream at all as this is an unnecessary step and as you have found out a potential bug. When you chain streams you only need to close the outer most stream, this closes all the chained streams in the correct order.
 
Campbell Ritchie
Marshal
Posts: 52587
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!