2015-02-24

[Java] InputStream to OutputStream

Stream Read / Write
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class StreamCopy1 {

    public static void main(String[] args) throws Exception {

        InputStream input = new FileInputStream("input.txt");
        OutputStream output = new FileOutputStream("output.txt");

        int byteValue;
        while ((byteValue = input.read()) != -1) {
            output.write(byteValue);
        }

        input.close();
        output.close();
    }
}


Apache IOUtils.copy
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;

public class StreamCopy2 {

    public static void main(String[] args) throws Exception {

        InputStream input = new FileInputStream("input.txt");
        OutputStream output = new FileOutputStream("output.txt");

        IOUtils.copy(input, output);

        input.close();
        output.close();
    }
}

沒有留言:

張貼留言

你好!歡迎你在我的 Blog 上留下你寶貴的意見。