【问题标题】:Append data into a file using Apache Commons I/O使用 Apache Commons I/O 将数据附加到文件中
【发布时间】:2011-02-27 07:45:06
【问题描述】:

Apache Commons I/O 的FileUtils.writeStringToFile(fileName, text) 函数会覆盖文件中的先前文本。我想将数据附加到我的文件中。有什么方法可以使用 Commons I/O 吗?我可以使用 Java 中的普通 BufferedWriter 来做到这一点,但我对使用 Commons I/O 也很好奇。

【问题讨论】:

  • 我认为没有预制的方法。你当然可以做 readLines(...).append(myLines) 但我想你说的是一个可能非常大的文件。
  • 是的!我有 27k 个文件,其中包含大约 900,00 个帖子。在这里寻找更多创新!
  • 对此有一个开放的功能请求 (issues.apache.org/jira/browse/IO-182)。不幸的是,它从 2008 年 9 月开始营业,没有任何迹象表明有人真的伸出手指来做这件事。
  • 自从写这篇文章以来,commons-io FileUtils 2.1 已经更新为支持附加到文件。 writeStringToFile(文件文件,字符串数据,布尔追加)
  • @DaSh 我已将其标记为已接受。

标签: java file-io apache-commons


【解决方案1】:

已在 Apache IO 2.1 版本中实现。 要将字符串附加到文件中,只需将 true 作为函数中的附加参数传递:

  • FileUtils.writeStringToFile
  • FileUtils.openOutputStream
  • FileUtils.write
  • FileUtils.writeByteArrayToFile
  • FileUtils.writeLines

例如:

    FileUtils.writeStringToFile(file, "String to append", true);

【讨论】:

  • 感谢您的回答。 +1
  • writeStringToFile() 的第三个位置应该有另一个参数:要使用的 Charset。当前版本的 Apache IO 报告上述方法已弃用。
【解决方案2】:

下载最新版本的Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

将追加设置为真......

【讨论】:

    【解决方案3】:

    小心。该实现似乎正在泄漏文件句柄...

    public final class AppendUtils {
    
        public static void appendToFile(final InputStream in, final File f) throws IOException {
            OutputStream stream = null;
            try {
                stream = outStream(f);
                IOUtils.copy(in, stream);
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }
    
        public static void appendToFile(final String in, final File f) throws IOException {
            InputStream stream = null;
            try {
                stream = IOUtils.toInputStream(in);
                appendToFile(stream, f);
            } finally {
                IOUtils.closeQuietly(stream);
            }
        }
    
        private static OutputStream outStream(final File f) throws IOException {
            return new BufferedOutputStream(new FileOutputStream(f, true));
        }
    
        private AppendUtils() {}
    
    }
    

    【讨论】:

    【解决方案4】:

    这个小东西应该可以解决问题:

    package com.yourpackage;
    
    // you're gonna want to optimize these imports
    import java.io.*;
    import org.apache.commons.io.*;
    
    public final class AppendUtils {
    
        public static void appendToFile(final InputStream in, final File f)
                throws IOException {
            IOUtils.copy(in, outStream(f));
        }
    
        public static void appendToFile(final String in, final File f)
                throws IOException {
            appendToFile(IOUtils.toInputStream(in), f);
        }
    
        private static OutputStream outStream(final File f) throws IOException {
            return new BufferedOutputStream(new FileOutputStream(f, true));
        }
    
        private AppendUtils() {
        }
    
    }
    

    编辑:我的 Eclipse 坏了,所以它之前没有向我显示错误。修复错误

    【讨论】:

      【解决方案5】:

      实际上,2.4 版的 apache-commons-io FileUtils 现在也具有集合的附加模式。

      Here's the Javadoc

      以及maven依赖:

      <dependency>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
          <version>2.4</version>
          <type>jar</type>
      </dependency>
      

      【讨论】:

        【解决方案6】:

        在 2.5 版本中,您需要传递一个额外的参数,即编码。

        FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);
        

        【讨论】:

        • 在JDK8中使用StandardCharsets.UTF_8代替字符串常量“UTF-8”。
        【解决方案7】:
        public static void writeStringToFile(File file,
                                             String data,
                                             boolean append)
                                      throws IOException
        
        
           Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
        
        Parameters:
            file - the file to write to
            lines - the lines to write, null entries produce blank lines
            append - if true, then the lines will be added to the end of the file rather than overwriting 
        Throws:
            IOException - in case of an I/O error
        Since:
            Commons IO 2.1
        

        【讨论】:

        • 这种情况下如何关闭输出流?
        猜你喜欢
        • 2012-04-02
        • 2012-10-05
        • 2010-12-09
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多