Saturday, 11 February 2023

Write InputStream to a file in Java

In this post, I am going to explain how to write the content of an InputStream to a file.

 


 

 

Approach 1: Using InputStream read method.

public static void writeInputStreamToAFile1(final InputStream inputStream, final String filePath)
        throws IOException {
    checkInput(inputStream, filePath);

    File newFile = new File(filePath);

    // create parent dirs if necessary
    newFile.getParentFile().mkdirs();
    newFile.createNewFile();

    try (FileOutputStream outputStream = new FileOutputStream(newFile, false)) {
        int read;
        byte[] bytes = new byte[65535];
        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    }

}

Approach 2: Using Files.copy() method.

public static void writeInputStreamToAFile2(final InputStream inputStream, final String filePath)
        throws IOException {
    checkInput(inputStream, filePath);

    File newFile = new File(filePath);

    // create parent dirs if necessary
    newFile.getParentFile().mkdirs();
    newFile.createNewFile();
    Files.copy(inputStream, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}

Approach 3: Using InputStream#transferTo method. This method is introduced in Java9.

public static void writeInputStreamToAFile3(final InputStream inputStream, final String filePath)
        throws IOException {
    checkInput(inputStream, filePath);

    File newFile = new File(filePath);

    // create parent dirs if necessary
    newFile.getParentFile().mkdirs();
    newFile.createNewFile();
    try (OutputStream output = new FileOutputStream(newFile, false)) {
        inputStream.transferTo(output);
    }
}

Find the below working application.

 

InputStreamToAFile.java

package com.sample.app;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class InputStreamToAFile {

    private static void checkInput(InputStream inputStream, String filePath) {
        if (inputStream == null) {
            throw new IllegalArgumentException("inputStream is null");
        }

        if (filePath == null) {
            throw new IllegalArgumentException("filePath is null");
        }
    }

    public static void writeInputStreamToAFile1(final InputStream inputStream, final String filePath)
            throws IOException {
        checkInput(inputStream, filePath);

        File newFile = new File(filePath);

        // create parent dirs if necessary
        newFile.getParentFile().mkdirs();
        newFile.createNewFile();

        try (FileOutputStream outputStream = new FileOutputStream(newFile, false)) {
            int read;
            byte[] bytes = new byte[65535];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }

    }

    public static void writeInputStreamToAFile2(final InputStream inputStream, final String filePath)
            throws IOException {
        checkInput(inputStream, filePath);

        File newFile = new File(filePath);

        // create parent dirs if necessary
        newFile.getParentFile().mkdirs();
        newFile.createNewFile();
        Files.copy(inputStream, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }

    public static void writeInputStreamToAFile3(final InputStream inputStream, final String filePath)
            throws IOException {
        checkInput(inputStream, filePath);

        File newFile = new File(filePath);

        // create parent dirs if necessary
        newFile.getParentFile().mkdirs();
        newFile.createNewFile();
        try (OutputStream output = new FileOutputStream(newFile, false)) {
            inputStream.transferTo(output);
        }
    }

    public static void main(String[] args) throws IOException {
        ByteArrayInputStream bios1 = new ByteArrayInputStream(new String("Hello World").getBytes());
        ByteArrayInputStream bios2 = new ByteArrayInputStream(new String("Hello World").getBytes());
        ByteArrayInputStream bios3 = new ByteArrayInputStream(new String("Hello World").getBytes());

        String file1 = "/Users/Shared/files/file1.txt";
        String file2 = "/Users/Shared/files/file2.txt";
        String file3 = "/Users/Shared/files/file3.txt";

        writeInputStreamToAFile1(bios1, file1);
        writeInputStreamToAFile1(bios2, file2);
        writeInputStreamToAFile1(bios3, file3);
    }

}

Run above application, you can confirm that three files file1, file2 and file3 created with the content of inputstream.



 

You may like

file and stream programs in Java

check whether a file is executable or not in Java

Check whether a directory has some files or not

Check given path is a file and not a symbolic link in Java

Check given path is a directory and not a symbolic link in Java

Convert InputStream to string

No comments:

Post a Comment