你能幫我解決下一個問題嗎? 我正在嘗試使用PrintStream和我的線程來使它們寫入不同的輸出。 的問題是:System.out的完美的作品,但是當我嘗試添加OutputStream不支持線程
File f1 = new File("src/task7/simple/1.txt");
PrintStream filePRinPrintStream = new PrintStream(f1)
我的主題不想寫東西,它拋出沒有警告或異常而普通的印刷是main()方法是OK 。
filePRinPrintStream.println("PREVED");
這裏是我的代碼摘錄您的信息。從另一個類
public static void main(String[] args) {
File f1 = new File("src/task7/simple/1.txt");
try(PrintStream filePRinPrintStream = new PrintStream(f1)){
//filePRinPrintStream.println("PREVED");
NamePrinterIF thread2 = new NamePrinterThread();
thread2.setCount(20);
thread2.setInterval(350);
thread2.setStream(filePRinPrintStream);
thread2.setPrintName("thread2");
thread2.startThread();
filePRinPrintStream.flush();
} catch(IOException e){
e.printStackTrace();
}
NamePrinterIF thread1 = new NamePrinterThread();
thread1.setCount(10);
thread1.setInterval(200);
thread1.setStream(System.out);
thread1.setPrintName("thread1");
thread1.startThread();
}
你在哪裏關閉你的printstream? – AlexWien