Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Reading/Writing Compressed and Not Compressed Files in Java

DZone's Guide to

Reading/Writing Compressed and Not Compressed Files in Java

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Main reason for this post is trying don’t repeat yourself (DRY) because, often, I fall in the recursive need to read and write compressed and not compressed files (mainly JSON and CSV).

Let’s to see first how to read text files. Note I’m working with (relatively small) text files so:

  1. The read methods returns an String with the whole content.
  2. I’m using BufferedReader to read line by line.
private String readFile(String fileName) {
    StringBuilder sb = new StringBuilder();
    try {
        BufferedReader input = new BufferedReader(new FileReader(new File(fileName)));
        try {
            String line = null;
            while ((line = input.readLine()) != null) {
                sb.append(line);
            }
        } finally {
            input.close();
        }
    } catch (IOException ex) {
        // Handle exception
        return null;
    }
 
    return sb.toString();
}

Note: there are more than one way to do things. In the entry Best way to read a text file, where you can find many different ways to read a text file depending on your JDK version and the size of the file.

Similarly to write a String to a file:

private void writeFile(String fileName, String value) {
    try {
        FileWriter fw = new FileWriter(fileName);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(value);
        bw.close();
    } catch (IOException ex) {
        // Handle exception
    }
}

To read/write compressed files, that is with binary data, we need to work with streams and buffers. So to read a GZIP compressed file and obtain a String:

private String readCompressedFile(String fileName) {
    try {
        GZIPInputStream gis = new GZIPInputStream(new FileInputStream(fileName));
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gis.read(buffer)) != -1) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        gis.close();
        return new String(fos.toByteArray());
    } catch (IOException ex) {
        // Handle exception
        return null;
    }
}

and similarly to write a String to a GZip compressed file:

private void writeCompressedFile(String fileName, String value) {
    try {
        InputStream is = new ByteArrayInputStream(value.getBytes());
        GZIPOutputStream gzipOS = new GZIPOutputStream(new FileOutputStream(fileName));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            gzipOS.write(buffer, 0, len);
        }
        gzipOS.close();
        is.close();
    } catch (IOException ex) {
        // Handle exception
    }
}


 

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of Antonio Santiago, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}