Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, January 10, 2020

How to convert InputStream to File in Java

1. Overview


In this tutorial, We'll learn how to convert or write an InputStream to a File.

This can be done in different ways as below.

A) Using plain Java
B) Using java 7 NIO package
C) Apache Commons IO library
D) Guava

How to convert InputStream to File in Java





2. Using plain Java API


Reading a file from a predefined location or from a memory on disk and writing the contents into a new file.

package com.javaprogramto.w3schools.programs.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * File Read an Write Example
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileReadWriteExample {

    public static void main(String[] args) {

        try {
            InputStream initialStream = new FileInputStream(new File("files/report.txt"));
            byte[] buffer = new byte[initialStream.available()];
            initialStream.read(buffer);

            File targetFile = new File("files/report-output.txt");
            OutputStream outStream = new FileOutputStream(targetFile);
            outStream.write(buffer);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}



This program runs successfully if the memory allows to read the file and write. Note here that the file location and contents fixed.

If you need to read the contents from a stream then you need to read the contents continuously as below until reaches the end of the stream.

package com.javaprogramto.w3schools.programs.files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

/**
 * File Read an Write Example
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileReadWriteExample {

    public static void main(String[] args) {
        try {
            URI u = URI.create("https://www.google.com/");
            InputStream initialStream = u.toURL().openStream();
            File targetFile = new File("files/google.txt");
            OutputStream outStream = new FileOutputStream(targetFile);

            try (FileOutputStream outputStream = new FileOutputStream(targetFile)) {

                int read;
                byte[] bytes = new byte[1024];

                while ((read = initialStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

3. Java NIO Package


Next, let us see the nio package solution.

package com.javaprogramto.w3schools.programs.files;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * File Read an Write Example using Files.copy
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileReadWriteExample {

    private static final String OUTPUT_FILE = "files/google.txt";

    public static void main(String[] args) {
        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            // Java 1.7 onwards
            Files.copy(inputStream, Paths.get(OUTPUT_FILE));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


4. Using Apache Commons IO Library


Next, a quicker solution using Apache Commons IO utility.

package com.javaprogramto.w3schools.programs.files;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.commons.io.FileUtils;

/**
 * File Read an Write Example using commons-io lib
 * 
 * @author JavaProgramTo.com
 *
 */
public class FileReadWriteExample {

    private static final String OUTPUT_FILE = "files/google.txt";

    public static void main(String[] args) {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            File file = new File(OUTPUT_FILE);

            // commons-io
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

5. Using Guava Language


Finally, a simple solution using Guava programming.

 InputStream initialStream = new FileInputStream(
      new File("files/sample.txt"));
    byte[] buffer = new byte[initialStream.available()];
    initialStream.read(buffer);
 
    File targetFile = new File("files/targetFile.tmp");
    Files.write(buffer, targetFile);

6. Conclusion


In this article, we've seen all possible and quickest ways to convert InputStream to a File.


No comments:

Post a Comment

Please do not add any spam links in the comments section.