Java Reference
In-Depth Information
PipedInputStream and PipedOutputStream.
For writing and reading
data between two threads.
InputStream and OutputStream. The abstract parent classes are often
used for physical connections to data sources (other than files, which use
the FileInputStream and FileOutputStream classes). For example, a
socket connection uses these classes to represent the input and output
streams of the socket.
Let's look at an example. The following EchoFile program uses the FileIn-
putStream class to read the elements from a file and print them out to the con-
sole output. Study the program and try to determine its output, which is
shown in Figure 16.3.
import java.io.*;
public class EchoFile
{
public static void main(String [] args)
{
File file = new File(args[0]);
if(!file.exists())
{
System.out.println(args[0] + “ does not exist.”);
return;
}
if(!(file.isFile() && file.canRead()))
{
System.out.println(file.getName()
+ “ cannot be read from.”);
return;
}
try
{
FileInputStream fis = new FileInputStream(file);
char current;
while(fis.available() > 0)
{
current = (char) fis.read();
System.out.print(current);
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Search WWH ::




Custom Search