当前位置: 首页 > 面试题库 >

在java中如何追加文本到存在的文件中?

娄德运
2023-03-14
问题内容

在java中如何追加文本到存在的文件中?


问题答案:

Java 7+

如果你只需要执行一次,则使用Files类很容易:

try {
    Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND);
}catch (IOException e) {
    //exception handling left as an exercise for the reader
}

注意:NoSuchFileException如果文件不存在,上述方法将抛出。它还不会自动追加换行符(追加到文本文件时通常会需要此换行符)。

但是,如果你要多次写入同一文件,则上述操作必须多次打开和关闭磁盘上的文件,这是一个缓慢的操作。在这种情况下,使用缓冲写入器更好:

try(FileWriter fw = new FileWriter("myfile.txt", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw))
{
    out.println("the text");
    //more code
    out.println("more text");
    //more code
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}

笔记:

  • FileWriter构造函数的第二个参数将告诉它追加到文件中,而不是写入新文件。(如果文件不存在,将创建它。)
  • BufferedWriter对于昂贵的作家(例如FileWriter),建议使用。
  • 使用a PrintWriter可访问println你可能习惯使用的语法System.out
  • 但是BufferedWriterPrintWriter包装器不是严格必需的。

较旧的Java

try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}

异常处理

如果你需要对旧版Java进行健壮的异常处理,它将变得非常冗长:

FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter out = null;
try {
    fw = new FileWriter("myfile.txt", true);
    bw = new BufferedWriter(fw);
    out = new PrintWriter(bw);
    out.println("the text");
    out.close();
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}
finally {
    try {
        if(out != null)
            out.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    try {
        if(bw != null)
            bw.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    try {
        if(fw != null)
            fw.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
}


 类似资料:
  • 问题内容: 嗨,我在写入或追加文件时没有问题,唯一的问题是,一旦我退出程序然后再次运行它,它就会创建一个新文件来覆盖我的原始文件。这是一个问题,因为我正在使用文本文件来保持运行状态。 有没有办法将已经创建的文本文件作为对象然后附加到它? 提前致谢。 问题答案: FileWriter有一个构造函数,它允许您设置带有布尔值的追加。 Java文档

  • 问题内容: 我发现os.Open()返回O_RDONLY文件,而os.Create()返回O_RDWR,但是找不到方法返回APPEND文件指针。 有什么帮助吗? 问题答案: 该OpenFile需要一个标志参数,您可以使用: 与O_CREATE一起使用,OpenFile还可以起到与os.Create()相同的作用。

  • 问题内容: 如何在注入Jenkins时将文本追加到文件中 我希望看到 25在哪里 这是我的尝试 错误: 版本:= 1.0。$ {env.BUILD_ID}:替换错误 注意文件在当前目录中 问题答案: 是一个groovy变量,而不是一个shell变量。由于您使用了单引号(),因此groovy 不会 替换字符串中的变量,并且Shell也不知道。您需要使用双引号并让groovy进行替换 或使用外壳程序知

  • 问题内容: 我用谷歌搜索了一段时间,但似乎找不到它,这应该很容易。我想将CR添加到使用Transformer创建的XML文件的末尾。有没有办法做到这一点> 我尝试了以下操作,但这导致空白文件? 问题答案: 简单…只需添加append选项:

  • 问题内容: 这是我需要帮助的功能: 原版的: 通缉: 问题答案: 以下完整示例将从当前目录中读取 现有 文件,追加新服务器,然后将文件重写为。没有现有的.xml文件,该功能将无法正常运行,因此您需要修改代码以处理这种情况。 示例 server.xml 文件: 对代码的主要更改是 未 创建新的“ root”元素。上面的示例仅使用现有节点中的当前根节点,然后追加一个新的 Server 元素并重新写入该

  • < code>fd.append("upload ",file)产生, 产生, 但我需要这个, 这是我的代码: