Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

DeflaterOutputStream not writing anything to text file after compression

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 121
12
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Maxim.. It worked .. just closed DeflaterOutputStream before FileOutputStream..
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 78435
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
…and welcome to the Ranch
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic