Beanio-如何写入流对象

发布于 2025-01-19 21:46:54 字数 1309 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2025-01-26 21:46:54

您似乎没有向 OutputstreamWriter 写入足够的数据,无法将一些数据推送到底层 ByteArrayOutputStream。这里你有两个选择。

  1. 手动刷新 writer 对象,然后将其关闭。然后,这会将数据写入outputStream。我不推荐这种方法,因为如果在手动刷新和关闭写入器之前出现任何异常,写入器可能不会被刷新或关闭。
  2. writer 使用 try-with-resource 块,它应该负责关闭 writer 并最终将数据刷新到 outputStream

这应该可以解决问题:

final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (final OutputStreamWriter writer = new OutputStreamWriter(outputStream) ) {
  final BeanWriter bw = factory.createWriter("sb", writer);

  final Team teamVO = new Team();
  teamVO.setName("TESTING");

  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
}

It looks like you haven't written enough data to the OutputstreamWriter for it to push some data to the underlying ByteArrayOutputStream. You have 2 options here.

  1. Flush the writer object manually and then close it. This will then write the data to the outputStream. I would not recommend this approach because the writer may not be flushed or closed should there be any Exception before you could manually flush and close the writer.
  2. Use a try-with-resource block for the writer which should take care of closing the writer and ultimately flushing the data to the outputStream

This should do the trick:

final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (final OutputStreamWriter writer = new OutputStreamWriter(outputStream) ) {
  final BeanWriter bw = factory.createWriter("sb", writer);

  final Team teamVO = new Team();
  teamVO.setName("TESTING");

  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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文