![](/img/trans.png)
[英]Stream Java audio to an icecast/shoutcast server? Pipes and stdin?
[英]Java: dealing properly with pipes as stdin
如果我停止從對非管道輸入正常運行的程序中讀取管道輸入,則會收到一個奇怪的錯誤(“進程嘗試向不存在的管道寫入數據。”)。 如何避免導致此錯誤?
碼:
package com.example.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PipeTest {
static public void main(String[] args) throws IOException
{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int i = 0;
while (i < 10)
{
String s = r.readLine();
if (s == null)
break;
++i;
System.out.println(i);
}
}
}
運行時輸出(testfile.txt只是一個超過10行的大文本文件):
C:\proj\java\test-pipe\bin>java com.example.test.PipeTest < ../testfile.txt
1
2
3
4
5
6
7
8
9
10
C:\proj\java\test-pipe\bin>type ..\testfile.txt | java com.example.test.PipeTest
1
2
3
4
5
6
7
8
9
10
The process tried to write to a nonexistent pipe.
該錯誤來自您的命令外殼,而不是Java程序。 抱怨是因為“類型”仍在嘗試寫入其輸出,但是當Java程序終止時,該管道突然關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.