Wednesday, September 2, 2015

Byte Streams

-------------------------Main.java------------------

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main
{
public static void main(String[] args) throws IOException
{
// Stream represents the input source and output destination
// InputStream is used to read data from the source and
// OutputStream is used to write data to the destination

// Byte Stream is used to perform input and output of 8-bit byte

FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("input.txt");
fileOutputStream = new FileOutputStream("output.txt");

int text ;
while((text = fileInputStream.read()) != -1){
fileOutputStream.write(text);
// Copying the text value of input.txt to output.txt
}
System.out.println("\nSuccessfully copied the text of input.txt to the output.txt file");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fileInputStream != null){
fileInputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
}
}
}

---------------------Output----------------------

No comments:

Post a Comment