【问题标题】:Java - appending to Excel file with FileOutputStreamJava - 使用 FileOutputStream 附加到 Excel 文件
【发布时间】:2010-12-20 10:17:16
【问题描述】:

此代码附加到已创建的 Excel 文件中:

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls");

我们可以添加/修改什么,以便在尚未创建的情况下创建 Decrypted.xls 并在已创建的情况下附加?

【问题讨论】:

  • 没有任何特定于 Excel 文件的内容。那也可以删掉。
  • 不知道这样做的目的是什么?不知何故,我怀疑结果是否会是一个有效的、更大的 Excel 文件,其中包含更多数据。

标签: java append fileoutputstream


【解决方案1】:

您需要FileOutputStream(File file, boolean append) 构造函数来切换是截断还是追加。

【讨论】:

    【解决方案2】:

    根据the Javadocs for the String-accepting constructor of FileOutputStream,rover12,如果文件不存在,则创建它。您没有看到这种行为吗?

    (正如其他人所提到的,请务必使用the constructor that takes the second boolean argument,这样您就可以指定要附加该文件(如果该文件已存在)...)

    【讨论】:

    • 不,我没有看到这种行为 .. 如果我删除已经存在的 Decrypted.xls,我会收到一个错误,说 .. 文件不存在
    【解决方案3】:

    使用构造函数:

    FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls", true);
    

    附加到现有文件(如果不存在)。您的示例将覆盖现有示例。

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 2014-05-09
      • 2015-02-13
      • 2018-01-18
      • 2011-01-14
      • 2015-04-11
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多