Java File.setReadable()在Windows上不起作用

时间:2019-05-27 16:09:37

标签: java linux windows

我有这段代码,当参数传递不可读的文件时,我想测试一个功能。该函数在这里无关紧要,只是File.setReadble(false)似乎仅在Windows上不起作用的事实。

File类已过时,那么如何保证文件是可读的还是独立于平台的呢?

我在Linux和Mac上测试了相同的代码,测试正常进行。

    @Test
    public void testReadableFile2() {
        System.out.println("testReadableFile2");
        URL resource = TestClass.class.getResource("/xml/file.xml");
        assertNotNull(resource);
        Path file = null;
        try {
            file = Paths.get(resource.toURI());
        } catch (final URISyntaxException ex) {
            System.out.println("exception: " + ex);
        }

        assertNotNull(file);
        assertTrue(Files.exists(file));

        // Query file system
        FileStore fileStore = null;
        try {
            fileStore = Files.getFileStore(file);
        } catch (final IOException ex) {
            System.out.println("IOException for get file store: " + ex);
        }

        assertNotNull(fileStore);

        boolean isReadable = true;
        if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
            boolean hadException = false;
            try {
                // Set read-only
                DosFileAttributeView att = Files.getFileAttributeView(file, DosFileAttributeView.class);
                att.setReadOnly(true);
                isReadable = !att.readAttributes().isReadOnly();
            } catch (final IOException ex) {
                System.out.println("IOException for set attribute: " + ex);
                hadException = true;
            }
            assertFalse(hadException);

        } else if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
            boolean hadException = false;
            try {
                // Change permissions
                PosixFileAttributes attrs = Files.readAttributes(file, PosixFileAttributes.class);
                Set<PosixFilePermission> newPermissions = attrs.permissions();
                newPermissions.remove(PosixFilePermission.OWNER_READ);
                newPermissions.remove(PosixFilePermission.OTHERS_READ);
                newPermissions.remove(PosixFilePermission.GROUP_READ);

                Files.setPosixFilePermissions(file, newPermissions);

                PosixFileAttributeView newAttr = Files.getFileAttributeView(file, PosixFileAttributeView.class);
                newAttr.setPermissions(newPermissions);

                boolean owner, others, group;

                owner = newAttr.readAttributes().permissions().contains(PosixFilePermission.OWNER_READ);
                others = newAttr.readAttributes().permissions().contains(PosixFilePermission.OTHERS_READ);
                group = newAttr.readAttributes().permissions().contains(PosixFilePermission.GROUP_READ);
                isReadable = !owner && !others && !group;
            } catch (final IOException ex) {
                System.out.println("io exception: " + ex);
                hadException = true;
            }
            assertFalse(hadException);
        }

        assertFalse(isReadable);

        assertFalse(Files.isReadable(file)); // assertion failure here
    }

我在最后一行出现断言失败。

有人可以帮助我解决这个问题吗?

0 个答案:

没有答案