2013-04-22 40 views
0

基本上,我試圖通過將現有的BufferedInputStream(System.in)切換到我自己的緩衝輸入流來實現擊鍵自動化。我想創建一個BufferedInputStream()對象,該對象附屬於可以異步控制的字符串。如何將bufferedInputStream集成到字符串?

這是我想要做什麼..

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

目前我能看到的唯一選擇是檢查文件是否存在,然後打開一個緩衝的輸入流吧。

我不太確定如何做到這一點,但我需要寫一些可以代替System.in inpustream的東西。

+1

您的問題很不清楚。你想寫'輸出'還是讀它? – 2013-04-22 21:50:19

+0

我不太明白你要做什麼。你想寫入StringBuilder,然後再讀取你從InputStream中寫入的內容嗎? – creechy 2013-04-22 23:06:20

+0

基本上,我有一個大型程序,有時我需要做一個大型程序的一部分。所以,我從原來的程序中劫掠stdin和stdout並重定向到讀者/作者:)答案在下面,謝謝閱讀。 – AdamOutler 2013-04-23 02:52:07

回答

0

我把一個良好的堅實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的緩衝讀寫器中。我可以讓它陷入困境,因爲它是一個公共靜態,這是可能的。現在,我可以像讀取和寫入我的應用程序一樣,在我自己的應用程序中運行命令行。

相關問題