Redian新闻
>
问一个blocking IO的程序
avatar
问一个blocking IO的程序# Java - 爪哇娇娃
M*r
1
Oracle Certification Has Engaged New Exam Delivery Vendor
Beginning September 25, 2009, Oracle certification exams will be offered
through a new test delivery vendor – Pearson VUE
avatar
n*1
2
我在网上看到这两段代码,一个写,一个读,但是好像是有问题的.我看写是没问题的
,我把queue打出来,输入的都有,但是reader的读行为非常奇怪,如果文件有内容,
可以读出来,但是重新写入的有时候能读一部分,比如writer写入abcde,可能read到ab
出来,但大部分时间都是null,尝试自己加过在writer加入sleep,wait什么都没用
Writer.java
import java.io.BufferedWriter;
import java.io.Console;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Writer {
private final BlockingDeque queue;
private final String filename;
public static void main(String[] args) throws Exception {
final Console console = System.console();
final Writer writer = new Writer("pipe");
writer.init();
while(true) {
String readLine = console.readLine();
writer.write(new StringBuffer(readLine));
}
}
public Writer(final String filename){
this.queue = new LinkedBlockingDeque();
this.filename = filename;
}
public void write(StringBuffer buf) {
queue.add(buf);
}
public void init() {
ExecutorService single = Executors.newSingleThreadExecutor();
Runnable runnable = new Runnable() {
public void run() {
while(true) {
PrintWriter w = null;
try {
String toString = queue.take().toString();
w = new PrintWriter(new BufferedWriter(new
FileWriter(filename)), true);
w.println(toString);
} catch (Exception ex) {
Logger.getLogger(Writer.class.getName()).log(Level.
SEVERE, null, ex);
}
}
}
};
single.submit(runnable);
}
}
Reader.java
public class Reader {
private final BufferedReader br;
public Reader(final String filename) throws FileNotFoundException {
br = new BufferedReader(new FileReader(filename));
}
public String readLine() throws IOException {
return br.readLine();
}
public void close() {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null,
ex);
}
}
public static void main(String[] args) throws FileNotFoundException {
Reader reader = new Reader("pipe");
while(true) {
try {
String readLine = reader.readLine();
System.out.println("readLine = " + readLine);
} catch (IOException ex) {
reader.close();
break;
}
}
}
}
avatar
c*d
3
果然是vue
beijing,有啥免费考试的coupon没,嘿嘿
avatar
g*g
4
You probably should flush on writer.
avatar
B*g
5
没有。nnd,公司都不给报销。

【在 c*****d 的大作中提到】
: 果然是vue
: beijing,有啥免费考试的coupon没,嘿嘿

avatar
n*1
6
我知道了,一个比较低级的错误,
w = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)), true
);
这里FileWriter需要加set append = true, 原来的true只是让PrintWriter auto
flush
我开始以为filereader是不是读的太快了,就算遇到空行也会往下读,实际上应该不会
,如果在print之前判断是否null,这样程序就演示的挺好了
谢谢了

【在 g*****g 的大作中提到】
: You probably should flush on writer.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。