Stream Input/Output
MS dot NET Streams
Disk input > FileStream > Memory Stream > Compression Stream > Crypto Stream > output
using System;
using System.IO;
class Class1
{
static void Main()
{
using (TextWriter w = File.CreateText("out.txt");
{
w.WriteLine ("Log: <{0}> {1:MM.dd.yy HH:MM}",
Environment.UserName, DateTime.Now);
}
using (TextReader r = File.OpenText ("out.txt");
{
Console.WriteLine ( r.ReadLine() );
}
}
}
Java Streams
This sample coding uses the FileInputStream and FileOutputStream classes from the
java.io package
to read data from a user-specified file to a buffer, and then out to standard output and a user-specified file.
import java.io.*;
public class myFileStreamsExample {
public static void main (String args[])
throws IOException {
File myFile = new File(args[0]);
File newFile = new File(args[1]);
System.out.println();
if(myFile.exists()) {
System.out.println(args[0] + " exists!\n");
FileInputStream inputFile = new FileInputStream(myFile);
// Copy entire input file to a buffer:
int bytesAvailable = inputFile.available();
byte[] buffer = new byte[bytesAvailable];
int bytes = inputFile.read(buffer);
System.out.println("Copying contents to " + args[1]);
FileOutputStream outputFile = new FileOutputStream(newFile);
outputFile.write(buffer); // writes entire file from buffer.
outputFile.close();
// Write input file to Standard System.Output once:
System.out.println("inputFile named " + args[0] + " has " +
bytesAvailable + " bytes in file" );
while(bytes != -1) {
System.out.write(buffer, 0, bytes); // 0 = offset
bytes = inputFile.read(buffer);
}
inputFile.close();
} else {
System.out.println(args[0] + " does not exist\n");
}
System.out.println();
}
}
The file input methods:
- available() - returns an int. This value is the number of bytes that can be read from the
FileInputStream object without
- close() - doesn't return anything. When the method is called, the FileInputStream object is closed, and all system resources associated with the stream are released.
- read() - returns an int that is the byte read from the file. This method will only read one byte at a time. When the end of the file is reached, a -1 is returned.
- read(byte[] buffer) - returns an int representing the number of bytes read into the buffer. When the end of the file is reached, a -1 is returned. This method will read data from the FileInputStream object and store it in an array of bytes which is specified as a parameter. It will continue reading until the end of the file, or until the array is full.
- read(byte[] buffer, int offset, int length) - works very similarly to the previous read method, except that you can specify the number of bytes to read (with the length parameter) and where in the FileInputStream object to start (with the offset parameter). When the end of the file is reached, a -1 is returned.
- skip(long bytes) - returns a long value representing the number of bytes that were actually skipped. When you call this method, you must specify, via the parameter, how many bytes you want to skip over before you start reading in the file.
Blocking occurs when there is no data available for a method to read.
It forces the thread in which the method is running to pause and wait for data to become available.
In multi-threaded programs, this isn't much to be concerned about, but you can see where it would be a problem in a program with only one thread.
Exceptions
IOException is a generic I/O exception—the father of all java.io package exceptions.
Others:
- EOFException - End of File or Stream has been reached.
- FileNotFoundException - No such File exists.
- InterruptedIOException - The I/O operation has been interrupted.
- IOException - A generic I/O exception has occurred, the father of all java.io package exceptions.
- NotSerializableException - An attempt has been made to read/write an object that can not be sent to a file or socket. (This is explained further in the following section.)
|
|