メインクラスに次のコードがあります。
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
System.in, System.out);
System.in はユーザーから入力を取得し、System.out はすべての出力を出力するため、これは非常にうまく機能します。
これを変更しようとしていたので、System.in の代わりに、入力を要求するたびにファイルから 1 行を読み取る別の InputStream オブジェクトにすることができます。また、System.out は、すべての出力をファイルに書き込むオブジェクトにすることもできます。
IOUtil クラスは次のとおりです。
package examples;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;
public final class IOUtil
{
public final static void readWrite(final InputStream remoteInput,
final OutputStream remoteOutput,
final InputStream localInput,
final OutputStream localOutput)
{
Thread reader, writer;
reader = new Thread()
{
public void run()
{
int ch;
try
{
while (!interrupted() && (ch = localInput.read()) != -1)
{
remoteOutput.write(ch);
remoteOutput.flush();
}
}
catch (IOException e)
{
//e.printStackTrace();
}
}
}
;
writer = new Thread()
{
public void run()
{
try
{
Util.copyStream(remoteInput, localOutput);
}
catch (IOException e)
{
e.printStackTrace();
System.exit(1);
}
}
};
writer.setPriority(Thread.currentThread().getPriority() + 1);
writer.start();
reader.setDaemon(true);
reader.start();
try
{
writer.join();
reader.interrupt();
}
catch (InterruptedException e)
{
}
}