JavaのInputStreamをFileに変換する方法

このチュートリアルでは、「 File to InputStream 」と「 InputStream to File 」の変換方法を示します。

1.ファイルをInputStreamに変換する

ファイルを `File`オブジェクトに読み込み、それを次のプロセスのために「入力ストリーム」に変換する方法は?

    File file = new File("/Users/mkyong/Downloads/file.js");

それを変換するには、 `FileInputStream`を使います。

    File file = new File("/Users/mkyong/Downloads/file.js");
    InputStream inputStream = new FileInputStream(file);

   //or
    InputStream inputStream =
          new FileInputStream("/Users/mkyong/Downloads/file.js");

完全な例を読む

FileToInputStreamApp.java

package com.mkyong.core;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileToInputStreamApp {
    public static void main(String[]args) {

    InputStream inputStream = null;
    BufferedReader br = null;

    try {
       //read this file into InputStream
        inputStream = new FileInputStream("/Users/mkyong/Downloads/file.js");

        br = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        System.out.println(sb.toString());
        System.out.println("\nDone!");

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
}

2. InputStreamをファイルに変換する

変換ではなく、 "Input Stream"を読み込み、それを `FileOutputStream`を介して別の新しいファイルに書き込む方法を示します

InputStreamToFileApp.java

package com.mkyong.core;

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

public class InputStreamToFileApp {
    public static void main(String[]args) {

    InputStream inputStream = null;
    OutputStream outputStream = null;

    try {
       //read this file into InputStream
        inputStream = new FileInputStream("/Users/mkyong/Downloads/holder.js");

       //write the inputStream to a FileOutputStream
        outputStream =
                    new FileOutputStream(new File("/Users/mkyong/Downloads/holder-new.js"));

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

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

        System.out.println("Done!");

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
               //outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    }
}