Wednesday, September 2, 2015

Character Stream

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

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main
{
public static void main(String[] args) throws IOException {

// Stream is used to perform input and output of 16-bit unicode.
// FileReader use the FileInputStream and FileWriter uses the FileOutputStream.
// FileReader read two byte at a time and FileWriter write two byte at a time.

FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");

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

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

No comments:

Post a Comment