Beanio-如何写入流对象
我正在尝试使用 beanio 创建固定长度的文件输出。我不想写入物理文件,而是想将内容写入 OutputStream。
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("sb")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addRecord(Team.class);
factory.define(builder);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
BeanWriter bw = sf.createWriter("sb", writer);
bw.write(teamVO) // teamVO has some value.
try(OutputStream os = new FileOutputStream("file.txt")){
outputStream.writeTo(os); //Doing this only to check outputStream has some value
}
这里创建的文件 file.txt 没有内容,大小为 0 kb。
我可以用以下方法写入文件,但由于我不想在物理位置写入文件,所以我想到将内容写入到outputStream中,然后用不同的方法将其转换为到文件
//This is working and creates file successfully
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("sb")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addRecord(Team.class)
factory.define(builder);
BeanWriter bw = factory.createWriter("sb", new File("file.txt"));
bw.write(teamVO);
为什么在第一种方法中创建的文件大小为 0 kb?
I am trying to create a fixed length file output using beanio. I don't want to write the physical file, instead I want to write content to an OutputStream.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("sb")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addRecord(Team.class);
factory.define(builder);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
BeanWriter bw = sf.createWriter("sb", writer);
bw.write(teamVO) // teamVO has some value.
try(OutputStream os = new FileOutputStream("file.txt")){
outputStream.writeTo(os); //Doing this only to check outputStream has some value
}
Here the file created, file.txt has no content in it, It is of size 0 kb.
I am able to write the file in the following method, but as I don't want to write the file in physical location, I thought of writing the contents in to a outputStream and then later in a different method, it can be converted in to a file
//This is working and creates file successfully
StreamFactory factory = StreamFactory.newInstance();
StreamBuilder builder = new StreamBuilder("sb")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addRecord(Team.class)
factory.define(builder);
BeanWriter bw = factory.createWriter("sb", new File("file.txt"));
bw.write(teamVO);
Why in the first approach the file is created with size 0 kb?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎没有向
OutputstreamWriter
写入足够的数据,无法将一些数据推送到底层ByteArrayOutputStream
。这里你有两个选择。writer
对象,然后将其关闭。然后,这会将数据写入outputStream
。我不推荐这种方法,因为如果在手动刷新和关闭写入器之前出现任何异常,写入器可能不会被刷新或关闭。writer
使用try-with-resource
块,它应该负责关闭writer
并最终将数据刷新到outputStream
这应该可以解决问题:
It looks like you haven't written enough data to the
OutputstreamWriter
for it to push some data to the underlyingByteArrayOutputStream
. You have 2 options here.writer
object manually and then close it. This will then write the data to theoutputStream
. I would not recommend this approach because thewriter
may not be flushed or closed should there be any Exception before you could manually flush and close thewriter
.try-with-resource
block for thewriter
which should take care of closing thewriter
and ultimately flushing the data to theoutputStream
This should do the trick: