简体   繁体   English

如何将BufferedWriter转换为BufferedReader

[英]How to convert a BufferedWriter to a BufferedReader

My understanding is that this is a common scenario, but Java doesn't have a baked in solution and I've been searching on and off for more than a day now. 我的理解是,这是一种常见的情况,但是Java尚没有内置的解决方案,而且我已经不停地搜索了一天以上。 I have tried the CircularCharBuffer from the Ostermiller library, but that uses some sort of reader that constantly waits for new input, so I couldn't get readline() to detect the end of the content (it would just hang). 我从Ostermiller库尝试了CircularCharBuffer,但是它使用某种读取器,该读取器不断等待新输入,因此我无法通过readline()来检测内容的结尾(它将挂起)。

So could someone tell me how I could do a conversion? 那么有人可以告诉我如何进行转换吗? For what it's worth, I'm converting multiple (potentially many) PDF files to raw text using the PDFBox lib. 对于它的价值,我正在使用PDFBox库将多个(可能很多)PDF文件转换为原始文本。 The PDFBox API puts the content onto a Writer , after which I need to get at the content for further processing (so BufferedReader/Writer is not actually essential, but some kind of Reader/Writer ). PDFBox API将内容放到Writer ,之后我需要获取内容以进行进一步处理(因此BufferedReader/Writer实际上并不是必不可少的,而是某种Reader/Writer )。 I know that this is possible using StringReader/Writer , but I'm not sure that this is efficient plus I loose the readline() method. 我知道使用StringReader/Writer ,但是我不确定这样做是否有效,而且我松了readline()方法。

This is a bit like asking how to convert a pig into an elephant ... :-) 这有点像问如何将猪变成大象... :-)

OK, there are two ways to address this problem (using the Java libraries): 好的,有两种方法可以解决此问题(使用Java库):

  • You can capture the data written to a buffered writer so that it can then be read using a buffered reader. 您可以捕获写入缓冲写入器的数据,以便随后可以使用缓冲读取器读取数据。 Basically, you do this by: 基本上,您可以通过以下方式进行操作:

    1. using your BufferedWriter to write to a StringWriter or CharArrayWriter, 使用您的BufferedWriter写入StringWriter或CharArrayWriter,

    2. closing it, 关闭它,

    3. extracting the resulting stuff from the SW / CAW as a String, and 从SW / CAW中提取生成的东西作为字符串,并且

    4. wrapping the String in a StringReader, 将String包裹在StringReader中,

    5. wrapping the StringReader in a BufferedReader. 将StringReader包装在BufferedReader中。

  • You can create a PipedReader / PipedWriter pair and wrap them with BufferedReader and BufferedWriter respectively. 您可以创建一个PipedReader / PipedWriter对,并分别用BufferedReader和BufferedWriter包装它们。

The two approaches both have disadvantages: 两种方法都有缺点:

  • The first one requires you to complete the writing before constructing the read side. 第一个要求您在构造读取面之前完成写作。 That means you need space to hold the entire stream content in memory, and you can't do producer-side and consumer-side processing in parallel. 这意味着您需要空间来将整个流内容保存在内存中,并且您无法并行进行生产者端和消费者端的处理。

  • The second one requires you to produce and consume in separate threads ... or risk having the pipeline block permanently. 第二个要求您在单独的线程中进行生产和使用...否则可能使管道永久阻塞。


Conceptually speaking, the Ostermiller library is really an reimplementation of PipeReader / PipeWriter. 从概念上讲,Ostermiller库实际上是PipeReader / PipeWriter的重新实现。 (And some of the advantages of his reimplementation were mooted in Java 1.6 ... which allows you to specify the pipeline's buffer size. Mark support is interesting, but I can imagine some problems, depending on how you used it.) (他重新实现的一些优点已在Java 1.6中讨论了...允许您指定管道的缓冲区大小。标记支持很有趣,但我可以想象到一些问题,具体取决于您如何使用它。)

You might also be able to find a PipedReader / PipedWriter replacement that uses a flexible buffer that grows and contracts as required. 您也许还可以找到使用灵活缓冲区的PipedReader / PipedWriter替换项,该缓冲区可以根据需要增长和收缩。 (At least ... this is conceptually possible.) (至少...从概念上讲这是可能的。)

The CircularCharBuffer from the Ostermiller lib has two methods getWriter() and getReader() to get a reader on the content of a writer, and vice versa. Ostermiller库中的CircularCharBuffer具有两个方法getWriter()getReader()来使读者了解作者的内容,反之亦然。 The reason the Reader was hanging at the final readLine() was because I wasn't calling close() on the writer after I had finished writing to it. Reader挂在最后的readLine()上的原因是因为我写完后没有在writer上调用close() So the final readLine() was waiting for new content on the writer that was never going to arrive. 因此,最终的readLine()正在等待写入器上永远不会到达的新内容。

The Ostermiller library can be found here . 奥斯特米勒图书馆可在此处找到。

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

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