更新时间:2022-07-14 GMT+08:00
分享

追加文件内容

功能简介

追加文件内容,是指在HDFS的某个指定文件后面,追加指定的内容。过程为:

  1. 使用FileSystem实例的append方法获取追加写入的输出流。
  2. 使用该输出流将待追加内容添加到HDFS的指定文件后面。

在完成后,需关闭所申请资源。

代码样例

如下是代码片段,详细代码请参考com.huawei.bigdata.hdfs.examples中的HdfsExample类。

/**
 * 追加文件内容
 *
 * @throws java.io.IOException
 */
private void append() throws IOException {
    final String content = "I append this content.";
    FSDataOutputStream out = null;
    try {
        out = fSystem.append(new Path(DEST_PATH + File.separator + FILE_NAME));
        out.write(content.getBytes());
        out.hsync();
        LOG.info("success to append.");
    } finally {
        // make sure the stream is closed finally.
        IOUtils.closeStream(out);
    }
}
分享:

    相关文档

    相关产品