When using Input or Output with java programming. We have serveral ways of implementing them.
At the smallest and lowest level possible is FileInputStream and FileOutputStream.
As the names describe one is for input the other is for output.
- Input - read files/read log
- Output - write files/print to log
- Read byte-by-byte
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileInputStream in2 = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
String as = "";
while ((c = in.read()) != -1) {
System.out.println("c is " + c + " & it is " + (char) c);
as = as + (char) c;
System.out.println("total\n'" + as + "'");
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
In xanadu.txt we have hello. Running the above method would produce.
c is 104 & it is h
total
'h'
c is 101 & it is e
total
'he'
c is 108 & it is l
total
'hel'
c is 108 & it is l
total
'hell'
c is 111 & it is o
total
'hello'
c is 10 & it is
total
'hello
'
c is 104 & it is h 104 is the ASCII code for character h.