Monday, June 1, 2015

Byte Stream

package byteStream;

// Stream can be defined as a sequence of data.
// Stream represents an input source and output destination.
// InputStream is used to read data from a source and OutputStream is used for
// writing data to a destination

//Though there are many classes related to character streams but the most frequently used
//classes are , FileInputStream and FileOutputStream..
// Java Byte Stream are used to perform input and output of 8-bit bytes.

// NOTES:
// Now let's have a file input.txt with the following content: "This is test for copy file".

// In this case the file is made inside src/outputs.txt .
// It reads the bytes data from input.txt and as an output it write the
// same text to output.txt by autogenerating the outputs.txt file.


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

public class ByteStream {

public static void main(String[] args) throws IOException {
FileInputStream fis = null;
FileOutputStream fos = null;

try {
fis = new FileInputStream("src/input.txt");
fos = new FileOutputStream("src/outputs.txt");

int c ;
while((c = fis.read())!= -1){
fos.write(c);
}
System.out.println(fos); // reference value of the file
}finally{
if (fis!= null) {
fis.close();
}
if (fos!= null) {
fos.close();
}
}
}
}

-------------------------------------------input.txt------------------------------------------

This is test for copy file.

-------------------------------------------OUTPUT----------------------------------------

  // In this case the file is made inside src/outputs.txt .
// It reads the bytes data from input.txt and as an output it write the
// same text to output.txt by autogenerating the outputs.txt file. 


No comments:

Post a Comment