如何写入存储在 Java 在线服务器上的.txt文件?



hey.我想使用 java 将特定的字符串写入我在 awardspace 上的域上托管的.txt文件。在奖励空间上,我编辑了权限,以便允许读取和写入。但是现在我只弄清楚如何从文件中读取,并且它可以工作。有没有办法写信给它?

可以使用简单的方法追加到文件。只需传入文件的路径和要写入文件的数据。

我通常使用 UTF-8 来确保正确显示任何特殊字符。

public static void appendToFileUtf8(String file, String data) {
try {
FileOutputStream fout = new FileOutputStream(file, true);
OutputStreamWriter outwrite = new OutputStreamWriter(fout, "UTF-8");
outwrite.write(data);
outwrite.close();
} catch (IOException e) {
throw new RuntimeException("Error writing to file: "+file+" "+e.getMessage());
}
}

最新更新