从文件输入流,到文件输出流

发布于 2024-11-07 03:25:15 字数 2180 浏览 0 评论 0原文

我在主类中有这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-11-14 03:25:15

要替换 System.out,只需使用 FileOutputStream 即可。

要替换 System.in,您可以使用 FileInputStream。这不会一次传递一行输入,但我希望远程 telnet 服务器可以处理提前输入,所以这应该不重要。

如果提前输入确实很重要,那么你就遇到了一个难题。 telnet 客户端的输入端必须与输出端同步,并等到远程服务器/shell 等待下一行后再发送它。

  • IOUtil.readWrite 方法(如所写)不会执行此操作。
  • 这样做需要输出端注意到命令提示符(或其他东西)并告诉输入端写入下一行输入。这很棘手……而且很脆弱,原因如下:
    • 您不确定命令提示符会是什么样子。
    • 其中一个命令可以即时更改命令提示符。
    • 命令输出可能看起来像 shell 命令提示符。

我简单地浏览了 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.

  • The IOUtil.readWrite method (as written) doesn't do that.
  • Doing it requires the output side to notice command prompts (or something) and tell the input side to write the next line of input. That's tricky ... and fragile for the following reasons:
    • You don't know for sure what the command prompt(s) will look like.
    • One of the commands could change the command prompts on the fly.
    • The command output could look like a shell command prompt.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文