|
import java.io.* ;
public class PipeApp
{
public st1atic void main(String[] args)
{
PipeApp pipeApp = new PipeApp();
try
{
FileInputStream XFileIn = new FileInputStream("input.txt");
InputStream YInPipe = pipeApp.changeToY(XFileIn);
InputStream ZInPipe = pipeApp.changeToZ(YInPipe);
System.out.println();
System.out.println("here are the results:");
System.out.println();
BufferedReader reader = new BufferedReader(new InputStreamReader(ZInPipe));
String str = reader.readLine();
while(str != null)
{
System.out.println(str);
str = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
public InputStream changeToY(InputStream inputStream)
{
try
{
BufferedReader XFileIn = new BufferedReader(new InputStreamReader(inputStream));
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
PrintWriter printWriter = new PrintWriter(pipeOut);
YThread yThread = new YThread(XFileIn , printWriter);
yThread.start();
return pipeIn;
}
catch(Exception e)
{
System.out.println(e.toString());
}
return null ;
}
public InputStream changeToZ(InputStream inputStream)
{
try
{
BufferedReader YFileIn = new BufferedReader(new InputStreamReader(inputStream));
PipedOutputStream pipeOut2 = new PipedOutputStream();
PipedInputStream pipeIn2 = new PipedInputStream(pipeOut2);
PrintWriter printWriter2 = new PrintWriter(pipeOut2);
ZThread zThread = new ZThread(YFileIn , printWriter2);
zThread.start();
return pipeIn2;
}
catch(Exception e)
{
System.out.println(e.toString());
}
return null;
}
}
********************************************************************************
import java.io.* ;
class YThread extends Thread
{
BufferedReader XFileIn;
PrintWriter printWriter;
YThread(BufferedReader XFileIn , PrintWriter printWriter)
{
this.XFileIn = XFileIn ;
this.printWriter = printWriter ;
}
public void run()
{
try
{
String XString = XFileIn.readLine();
while(XString !=null)
{
String YString = XString.replace('X','Y');
printWriter.println(YString);
printWriter.flush();
XString = XFileIn.readLine();
}
printWriter.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
}
********************************************************************************
import java.io.* ;
class ZThread extends Thread
{
BufferedReader YFileIn ;
PrintWriter printWriter;
ZThread(BufferedReader YFileIn ,PrintWriter printWriter)
{
this.YFileIn = YFileIn ;
this.printWriter = printWriter ;
}
public void run()
{
try
{
String YString = YFileIn.readLine();
while(YString != null)
{
String ZString = YString.replace('Y','Z');
printWriter.println(ZString);
printWriter.flush();
YString = YFileIn.readLine();
}
printWriter.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
}
就一将X换Y,Y换Z的程序,搞這么zhang,看wan累半死。。。不过對了解P ipedInputStream ,线程还是不错a。
欢迎lai到Java学习者论坛,转载請zhu明de址:http://www.javaxxz.com. |
|