Copy a text file using InputStream and OutputStream - Java File Path IO

Java examples for File Path IO:FileOutputStream

Description

Copy a text file using InputStream and OutputStream

Demo Code


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

public class OuputStreamTest {
    public static void main(String[] args) throws IOException{
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try{//from w  ww  .  j  av  a  2s.co m
            inputStream = new FileInputStream("C:/input.txt");
            outputStream = new FileOutputStream("C:/output.txt");
            byte[] bytes = new byte[1024];
            int index;
            while((index = inputStream.read(bytes)) != -1){
                System.out.println("index ="+index);
                outputStream.write(bytes, 0, index);
            }
        }finally{
            if(outputStream != null){
                outputStream.close();
           }
            if(inputStream != null){
                inputStream.close();
            }
        }
    }
}

Related Tutorials