|
Previous Next
Doing Byte IO
Here we will use the classes FileInputStream and FileOutputStream of the package java.io to read from and write to files. These 2 classes allow you to read or write one or more bytes to and from files. The bytes are type integer.
The example below shows how to copy a file to another, one byte at a time. It also displays all numeric characters in the file. The character 0 has an integer value of 48 and the character 9 has an integer value of 57. So while copying, anytime we find a character in this range, we will display it.
The input and output file to use should be entered in the parameters for the main method, for arguments[0] and arguments[1].
import java.io.*;
public class StreamIO {
public static void main(String[] arguments) {
if (arguments.length < 2)
System.out.println
("Usage: java StreamIO filename1 filename2");
else {
try {
File inFile = new File(arguments[0]);
FileInputStream in = new FileInputStream(inFile);
File outFile = new File(arguments[1]);
FileOutputStream out = new FileOutputStream(outFile);
boolean eof = false;
while (!eof) {
int input = in.read();
if (input == -1)
eof = true;
else
out.write(input);
if (input > 47 && input < 58)
System.out.println
(" num=" + input + " char="
+ (char)input);
}
in.close();
out.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}
}
In this example, we will read a group of 10 bytes after we read an equal sign. Then we will write these 10 bytes to an output file. In the writing, at the end of each group of 10 bytes, we will write a carriage return and line feed to force the next write to occur on a new line.
int 10 = line feed
int 13 = carriage return
int 61 = equal sign
import java.io.*;
public class StreamIOMulti {
public static void main(String[] arguments) {
byte b[] = new byte[10];
int ByteCnt;
if (arguments.length < 2)
System.out.println
("Usage: java StreamIOMulti filename1 filename2");
else {
try {
File inFile = new File(arguments[0]);
FileInputStream in = new FileInputStream(inFile);
File outFile = new File(arguments[1]);
FileOutputStream out = new FileOutputStream(outFile);
boolean eof = false;
while (!eof) {
int input = in.read();
if (input == -1)
eof = true;
else
if (input == 61) {
ByteCnt = in.read(b);
out.write(b);
out.write(13);
out.write(10);
}
}
in.close();
out.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}
}
In most cases, the CPU processor is many times faster than the IO devices on a machine. The IO devices would include things such as printers, keyboards, disk files, etc. Buffering input and output allows data to and from these devices to be stored in memory which is faster than most IO devices, even though still slower than the processor. Then the processor can access memory at a greater speed than dealing with the IO devices. The classes BufferedInputStream and BufferedOutputStream can be used to buffer the data to and from the IO devices to take advantage of the extra speed advantage.
The following source is the same as the first one on this page except we've added the use of the classes BufferedInputStream and BufferedOutputStream.
import java.io.*;
public class StreamIOBuff {
public static void main(String[] arguments) {
if (arguments.length < 2)
System.out.println
("Usage: java ReadJava filename1 filename2");
else {
try {
File inFile = new File(arguments[0]);
FileInputStream in =
new FileInputStream(inFile);
BufferedInputStream buffi =
new BufferedInputStream(in);
File outFile = new File(arguments[1]);
FileOutputStream out =
new FileOutputStream(outFile);
BufferedOutputStream buffo =
new BufferedOutputStream(out);
boolean eof = false;
while (!eof) {
int input = buffi.read();
if (input == -1)
eof = true;
else
buffo.write(input);
if (input > 47 && input < 58)
System.out.println
(" num=" + input + " char="
+ (char)input);
}
buffi.close();
buffo.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
}
}
}
Previous Next
|