简体   繁体   English

Java - 来自缓冲读卡器的打印线

[英]Java - printing lines from buffered reader

I'm writing an InputStream that supplies lines from a file in constant intervals. 我正在写一个InputStream ,它以恒定的间隔从文件中提供行。 I used BufferedReader before, but ran into buffering issues with it (wasn't getting anything until the entire file was read), and speed isn't an issue anyways (the intervals are something like every second, or every half second - along those lines). 之前我使用过BufferedReader ,但遇到了缓冲问题(直到读完整个文件才得到任何东西),速度也不是问题(间隔类似于每秒,或每半秒 - 沿着那些线)。 Is there a class with a readLine method like in BufferedReader , except unbuffered? 是否有类似BufferedReaderreadLine方法,除了无缓冲?

(Edit: I just checked - my class seems to work, apparently the problem was with the output) (编辑:我刚刚检查过 - 我的课似乎工作,显然问题出在输出上)

Here's the code where I used the stream ( OnlineDataSimulator ). 这是我使用流的代码( OnlineDataSimulator )。 I already checked, the stream does exactly what I want, so apparently I'm doing something wrong with the output. 我已经检查过了,流确实完全符合我的要求,所以显然我输出的内容有问题。 (The actual problem is, I want output to occur every X milliseconds - X being the second parameter to OnlineDataSimulator . What happens when I run this code is, that I first get an X*lines wait and then the entire output at once instead.) (实际问题是,我希望输出每X毫秒发生一次 - X是OnlineDataSimulator的第二个参数。当我运行这个代码时会发生什么,我首先得到一个X *行等待,然后立即转换整个输出。 )

        System.out.println("Testing:");
        PrintStream fout = new PrintStream(new FileOutputStream("testfile"));
        for(int i=0; i<20; ++i) {
            fout.println(i);
        }
        fout.close();
        BufferedReader fin = new BufferedReader(new InputStreamReader(
                new OnlineDataSimulator("testfile",250)));
        String line;
        while((line=fin.readLine())!= null){
            System.out.println(line);
            System.out.flush();
        }
        fin.close();
        (new File("testfile")).delete();

Try it this way.... This worked for me.. 试试这种方式......这对我有用..

File f = new File("path");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

String s = null;

while ((s=br.readLine())!=null)
    {

           System.out.println(s);
    }

No, there is no other non-buffered option. 不,没有其他非缓冲选项。 A solution would be to write your own Reader which has a InputStreamReader as an underlying stream and in the readLine() method you should call read() of the underlying input stream reader until "\\n" is found. 一个解决方案是编写自己的Reader,它有一个InputStreamReader作为底层流,在readLine()方法中你应该调用底层输入流读取器的read(),直到找到“\\ n”。 Aggregate all these and return them as a string. 聚合所有这些并将它们作为字符串返回。

If you don't want to have a real buffer but want to use the functionality of BufferedReader you could initialize it with buffer size 1. As you commented that speed isn't an issue maybe is the most reliable solution. 如果您不想拥有真正的缓冲区但想要使用BufferedReader的功能,您可以使用缓冲区大小1初始化它。当您评论速度不是问题时,可能是最可靠的解决方案。

new BufferedReader(reader, 1)

public BufferedReader(Reader in, int sz) public BufferedReader(Reader in,int sz)

and you can check the readLine() method source code here , in case you want to implement your own. 你可以在这里查看readLine()方法的源代码,以防你想要实现自己的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM