I can't read a compressed file

We have to write a compressed file first with DeflaterOutputStream and than we have to read it back with the InflaterInputStream. I always get an EOFException when i try to read the line.

this is my java-code:

package oef5;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class oef5 {

    public static void main(String[] args) {
        Path pad =Paths.get("C:\\Users\\11402457\\Desktop\\school\\java advanced\\H05 - Lezen en schrijven\\oef5.txt");
        FileOutputStream out;
        try {
            out = new FileOutputStream("C:\\Users\\11402457\\Desktop\\school\\java advanced\\H05 - Lezen en schrijven\\oef5.txt");
        DeflaterOutputStream file = new DeflaterOutputStream(out);
        PrintStream print = new PrintStream(file);
        ObjectOutputStream out2 = new ObjectOutputStream(print);
        out2.writeObject("Dries");

        FileInputStream in = new FileInputStream("C:\\Users\\11402457\\Desktop\\school\\java advanced\\H05 - Lezen en schrijven\\oef5.txt");
        InflaterInputStream file2 = new InflaterInputStream(in);
        InputStreamReader reader = new InputStreamReader(file2);
        BufferedReader br = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        sb.append(br.readLine());
        String string = sb.toString();
        System.out.println(string);
        } 
        catch (IOException e) {
            e.printStackTrace();
        }

these are my exceptions:

java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(Unknown Source)
    at java.util.zip.InflaterInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at oef5.oef5.main(oef5.java:33)

I've been trying a lot but nothing seems to work, I guess I'm doing something wrong with my Stream.

Jon Skeet
people
quotationmark

You have three problems here:

  • You're using a PrintStream for no reason, and that will swallow exceptions.
  • You're writing data using an ObjectOutputStream, i.e. creating a binary file of Java serialized objects, but you're trying to read it using an InputStreamReader, as if it were a plain text file (but compressed)
  • You're not closing any of the streams when you've finished writing, before you try to read

If you're only trying to read and write text, you should probably just use an OutputStreamWriter (or a BufferedWriter wrapping it) to write to, rather than an ObjectOutputStream. Use try-with-resources statements to close the streams automatically:

try (FileOutputStream file = new FileOutputStream(out),
    DeflaterOutputStream deflater = new DeflaterOutputStream(file),
    OutputStreamWriter writer = new OutputStreamWriter(deflater, StandardCharsets.UTF_8),
    BufferedWriter bufferedWriter = new BufferedWriter(writer)) {
  // Write to bufferedWriter here
}

Then read it in a similar way, but using input streams and readers.

people

See more on this question at Stackoverflow