Skip to content

Instantly share code, notes, and snippets.

@ndw
Created September 5, 2021 08:47
Show Gist options
  • Save ndw/b2a8f5c6a3a8880ac72765b5672c8290 to your computer and use it in GitHub Desktop.
Save ndw/b2a8f5c6a3a8880ac72765b5672c8290 to your computer and use it in GitHub Desktop.
Java program to demonstrate that NIO will delete files that are read-only
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;
public class NioTest {
public static void main(String[] argv) throws Exception {
System.out.println("Hello, world");
Path foo = Paths.get("foo");
if (Files.exists(foo)) {
System.out.println("Foo exists: " + Files.exists(foo));
} else {
// Make the test file
new FileOutputStream("foo").close();
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.GROUP_READ);
perms.add(PosixFilePermission.OTHERS_READ);
Files.setPosixFilePermissions(foo, perms);
}
// Buh bye
Files.delete(foo);
}
}
@reinhapa
Copy link

reinhapa commented Sep 6, 2021

A day when I learn something, is a good day 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment