gpt4 book ai didi

java - 使用 UTF-8 编码创建并写入文件

转载 作者:行者123 更新时间:2023-12-01 19:01:06 26 4
gpt4 key购买 nike

这是我的 Java 代码。

File file = new File(path);
StringWriter sw = new StringWriter();
//Do something.
out.println(sw.toString()); //Works fine; prints.
try {
FileUtils.writeStringToFile(file, sw.toString(), "UTF-8");
} catch (IOException e) {
throw new RuntimeException( e );
}

我还没有创建文件,执行后也没有创建它。我怎样才能做到这一点?

最佳答案

参见File.createNewFile() .

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. ..

正如@JohnWatts 在评论中提到的:

..both PrintWriter and your code create the file, but pre-1.3 FileUtils.writeStringToFile does not.

关于java - 使用 UTF-8 编码创建并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12262329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com