Created
September 5, 2021 08:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A day when I learn something, is a good day 😊