Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 74783
336
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic