I would expect exception to be thrown in this code at line: charSink.write("hi world");
since the file is set to be non writable. But this code executes without any exceptions. How to make CharSink respect file permissions?
public class CharSinkWritingNonWritableFile {
public static void main(String[] args) throws Exception {
final File file = getNewFile();
file.setWritable(false, false);
val charSink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
file.setWritable(false, false);
charSink.write("hi world");
Files.readLines(file, Charsets.UTF_8).forEach(line -> {
System.out.println(line);
});
}
private static File getNewFile() {
final File file = new File("/tmp/hi-world");
if (file.exists()) {
file.delete();
}
return file;
}
}
Edit:
OS: MacOS 12.4