【问题标题】:How to attatch a bufferedInputStream to a string?如何将 bufferedInputStream 附加到字符串?
【发布时间】:2013-04-23 10:48:18
【问题描述】:

基本上,我试图通过将现有的 BufferedInputStream(System.in) 切换到我自己的缓冲输入流来自动化击键。我想制作一个 BufferedInputStream() 对象,该对象附加到可以异步控制的字符串之类的东西上。

这就是我想做的事情..

StringBuilder stringBuilder=new StringBuilder;
BufferedInputStream output=new something(new something(stringBuilder));

目前我能看到的唯一选择是检查文件是否存在,然后打开一个缓冲的输入流。

我不太确定该怎么做,但我需要写一些可以代替 System.in inpustream 的东西。

【问题讨论】:

  • 你的问题很不清楚。你想写信给output还是读它?
  • 我也不太明白您要做什么。您是否要写入 StringBuilder,然后再从 InputStream 中读取您写入的内容?
  • 基本上,我有一个大型程序,有时我需要成为大型程序的一部分。所以,我从原始程序中劫持标准输入和标准输出并重定向到读者/作者:) 答案如下,感谢阅读。

标签: java buffer inputstream


【解决方案1】:

我为此投入了 10 个小时的时间……这就是我想出的……管道流是我所需要的

    int BUFFER = 4096;
    PipedOutputStream toAppPipedOutputStream;
    PipedInputStream toAppPipedInputStream;

    static BufferedReader fromAppBufferedReaderfinal;
    BufferedOutputStream fromAppOutputStream;
    PipedOutputStream fromAppPipedOutputStream;
    PipedInputStream fromAppPipedInputStream;

    /* constructor sets up logging and parameters */

           try { 
                fromAppPipedInputStream = new PipedInputStream(BUFFER);
                fromAppOutputStream=new BufferedOutputStream(new PipedOutputStream(fromAppPipedInputStream));
                fromAppBufferedReaderfinal=new BufferedReader(new InputStreamReader(fromAppPipedInputStream));
                CASUAL.Log.out = new PrintStream(fromAppOutputStream);

                toAppPipedInputStream = new PipedInputStream(BUFFER);
                toAppPipedOutputStream=new PipedOutputStream(toAppPipedInputStream);
                CASUAL.CASUALInteraction.in= new BufferedReader(new InputStreamReader(toAppPipedInputStream));

在我的日志和输出类中,默认情况下,我正在写入指向 System.in 的缓冲读取器/写入器。我可以劫持它,因为它是一个公共静态,这是可能的。现在我可以读写我的应用程序,就好像它在我自己的应用程序内的命令行上运行一样

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    相关资源
    最近更新 更多