简体   繁体   中英

In Windows Java, how do I unset read only from a folder and all its contents?

Say I have a folder C:\\stuff . In stuff , there are thousands of miscellaneous files and other folders.

In Windows, I would simply just go to the folder, right click it and click properties, unset the read only flag, and apply the operation to the parent folder, all subfolders, and all files.

However, I want to automate this in Java with the simple click of a JButton in a GUI. What would the code inside the ActionListener look like? Is it even possible to do this? Is there something I can exploit, such as making Java write to PowerShell or cmd ?

You can do this using Files.walkFileTree , but if you do that in your ActionListener (or any method called from your ActionListener), your application will be frozen while the files are being changed. This is because the ActionListener is called on the event dispatch thread, which is the same thread on which keyboard input, mouse input, and screen painting is done. So while you perform a time-consuming operation in that thread, your application will ignore keyboard input and mouse input, and if it is visually obscured by any other applications which are then moved or closed, it will not redraw itself.

If you don't care whether your application hangs, you can just do the operation in the ActionListener:

Files.walkFileTree(rootDirectory, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path dir,
                                             BasicFileAttributes attr)
    throws IOException {
        Files.setAttribute(dir, "dos:readonly", false);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file,
                                     BasicFileAttributes attr)
    throws IOException {
        Files.setAttribute(file, "dos:readonly", false);
        return FileVisitResult.CONTINUE;
    }
});

But really, you should not hang the event dispatch thread. File I/O should be done in another thread. There are many ways to do that, but SwingWorker seems to be easiest for many people:

static void updateReadOnlyInTree(final Path root,
                                 Window parentWindow) {

    SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
        @Override
        public Void doInBackground()
        throws IOException {

            Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(
                                                Path dir,
                                                BasicFileAttributes attr)
                throws IOException {
                    Files.setAttribute(dir, "dos:readonly", false);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file,
                                                 BasicFileAttributes attr)
                throws IOException {
                    Files.setAttribute(file, "dos:readonly", false);
                    return FileVisitResult.CONTINUE;
                }
            });

            return null;
        }
    };

    final JOptionPane waitPane = new JOptionPane("Updating files\u2026",
        JOptionPane.INFORMATION_MESSAGE);
    final JDialog waitDialog = 
        waitPane.createDialog(parentWindow, "Files Update");

    worker.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent event) {
            if (event.getPropertyName().equals("state") &&
                event.getNewValue() == SwingWorker.StateValue.DONE) {

                waitPane.setValue(JOptionPane.CLOSED_OPTION);
            }
        }
    });

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            waitDialog.setVisible(true);
        }
    });

    worker.execute();
}

You could even go a step farther and count the files before changing any of them, then use a ProgressMonitor instead of a JOptionPane.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM