import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.Arrays;
import java.util.List;

public class JDK8168829 {

    private static final byte[] buf = new byte[1024];

    private static void execCmd(String cmd) throws Throwable {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(cmd);
        try (InputStream input = proc.getInputStream()) {
            int n;
            while ((n = input.read(buf)) != -1) {
                String s = new String(buf, 0, n, "US-ASCII");
                System.out.printf("%s", s);
            }
        }
        proc.waitFor();
    }

    public static void main(String[] args) throws Throwable {
        String _FOLDER = "D:\\dev1\\data\\test";
        String _FILE = "D:\\dev1\\data\\test\\.md.truc";

        // 1. create dir
        File folder = new File(_FOLDER);
        boolean res = folder.mkdirs();
        System.out.println("Directory created? " + res);
        execCmd("C:\\windows\\system32\\icacls " + _FOLDER);

        // 2. touch acl of the folder
        // note: if this is done AFTER the file creation, then no issue...
        new JDK8168829().touchAcl(folder.toPath());
        execCmd("C:\\windows\\system32\\icacls " + _FOLDER);

        // 3. create file
        Path fpath = Paths.get(_FILE);
        List<String> lines = Arrays.asList("The first line", "The second line");
        Files.write(fpath, lines, Charset.forName("UTF-8"));
        System.out.println("File written");

        // 4. read
        System.out.println("can read folder = " + folder.canRead());
        System.out.println("can read file = " + fpath.toFile().canRead());

        try (FileInputStream is = new FileInputStream(fpath.toFile())) {
            System.out.println("success opening file");
            is.close();
        } catch (IOException e) {
            System.out.println("exception opening file : " + e.getMessage());
            throw e;
        }
    }

    public void touchAcl(Path path) throws IOException {
        System.out.println("touching Acl at: " + path.toString());
        AclFileAttributeView aclFileAttributes = java.nio.file.Files.getFileAttributeView(path, AclFileAttributeView.class);
        aclFileAttributes.setAcl(aclFileAttributes.getAcl());
    }
}
