我想在這裏實現的是通過使用apache-commons打印輸出流的內容來獲得更多調試輸出。似乎我正在改變流程本身。在System.out上打印InputStream,而不更改使用apache commons的流
InputStream is = getClass().getResourceAsStream("file.txt");
IOUtils.copy(is, System.out); //Happily Prints out contents of file.txt
IOUtils.copy(is, System.out); //Doesn't print anything
爲什麼使用IOUtils複製流更改流?我試圖克隆流,然後打印出來,但仍然沒有運氣。我嘗試從Apache公共場景克隆的CloseShieldInputStream。
InputStream is = getClass().getResourceAsStream("file.txt");
CloseShieldInputStream csis = new CloseShieldInputStream(is);
IOUtils.copy(csis, System.out);//Happily Prints out contents of file.txt
IOUtils.copy(is, System.out);//Still Doesn't print anything
有人可以解釋爲什麼他們稱這些方法'複製'即使源流丟失其內容?如何打印出流而不用擔心丟失內容?
編輯:
此代碼(除流被初始化)是相當深的第三方庫內部和流通過幾種方法去後通過。很難確定流的初始化位置,並在那裏顯示並重新初始化。我拼命試圖顯示這個流,並仍然保持不變。
你不能。一般來說,一個'InputStream'只能從一次讀取。如果您想再次使用其內容,則需要重新打開該流。 –