제 코드에서는 두 번째 단어부터 시작해서 그 반대의 단어를 모두 사용해야합니다. 다른 클래스/스레드는 자신의 일을 잘하고 있습니다. 출력을 콘솔 창에 인쇄 할 수 있으며 올바르게 출력됩니다. 그러나 텍스트 파일에 쓰려고 할 때마다 바탕 화면에 출력물이 생성되지 않습니다.BufferedWriter가 파일에 대한 출력을 생성하지 않습니다.
내 코드 :
package ProducerConsumerAssignment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
/**
*
* @author Tyler Weaver
*/
public class WordWriter implements Runnable {
private final String END_FLAG = "Terminate the queue";
private static BlockingQueue<CharSequence> in;
private final File output;
/**
* Constructs a WordWriter object
*
* @param file the file to write words to
* @param queue the blocking queue to retrieve words from
*/
public WordWriter(final File file, BlockingQueue queue) {
output = file;
in = queue;
}
/**
* Executes when being called in a thread
*/
@Override
public void run() {
boolean isInterrupted = false;
while (!isInterrupted) {
try (BufferedWriter out = new BufferedWriter(new FileWriter(output))) {
CharSequence word = in.take();
if (word.toString().equalsIgnoreCase(END_FLAG))
Thread.currentThread().interrupt();
out.write(word.toString() + " ");
System.out.printf("%s%n", word);
} catch (IOException ex) {
System.err.printf("Error closing the file!%n%s%n", ex);
} catch (InterruptedException ex) {
isInterrupted = true;
}
}
}
}
사용중인 java 버전은 무엇입니까? 당신은 작가를 autoclose하기 위해 try-with를 사용하고 있습니까? http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – RNJ
Java 1.8이 있습니다. 예, 나는 autoclose 작가를 시도하고있었습니다. –
당신은 out.write (...) 후에 플러시를 호출해야합니다 – bestsss