从文件输入流,到文件输出流
我在主类中有这段代码:
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
System.in, System.out);
这非常有效,因为 System.in 从用户获取输入,而 System.out 打印所有输出。
我试图改变这一点,因此 System.in 可以是另一个每次请求输入时从文件中读取一行的 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)
{
}
}
I have this code in the main class:
IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(),
System.in, System.out);
This works very well, since System.in gets the inputs from the user nad System.out prints all the outputs.
I was trying to change this, so instead of System.in could be another InputStream object that reads one line from a file each time asks for an input, and also System.out could be an Object that writes all the output to a file.
the IOUtil class is the following:
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)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要替换 System.out,只需使用
FileOutputStream
即可。要替换 System.in,您可以使用
FileInputStream
。这不会一次传递一行输入,但我希望远程 telnet 服务器可以处理提前输入,所以这应该不重要。如果提前输入确实很重要,那么你就遇到了一个难题。 telnet 客户端的输入端必须与输出端同步,并等到远程服务器/shell 等待下一行后再发送它。
IOUtil.readWrite
方法(如所写)不会执行此操作。我简单地浏览了 telnet 协议,没有看到任何表明客户端必须一次发送一行数据的内容。
For the replacement for System.out, simply use a
FileOutputStream
.For the replacement for System.in, you could use a
FileInputStream
. This doesn't deliver input a line at a time, but I expect that the remote telnet server can handle type-ahead, so that shouldn't matter.If type-ahead does matter, then you've got a difficult problem. The input side of your telnet client has to synchronize with the output side, and wait until the remote server / shell is expecting the next line before sending it.
IOUtil.readWrite
method (as written) doesn't do that.I had a brief look at the telnet protocol, and I could see nothing that says that the client has to send data a line at a time.