Check with Java / Kotlin that files cannot be written in UAC on Windows

TL;DR If you want to support Windows, you should use java.nio.file.Files.isWritable.

Introduction

I didn't know anything about the internet, so I wrote a memo for myself.

The target is the process of saving a file in Java / Kotlin, especially the process of letting the user select a directory and saving it.

I'm talking about Java classes, but since I actually did Kotlin, the sample source follows that.

How to check write permission

There are two ways to check. The old way and the relatively new way.

  1. java.io.File.canWrite

  2. java.nio.file.Files.isWritable

  3. is an instance method of the good old java.io.File class.

  4. is a static method of a relatively new class added in Java 1.7 called java.nio.file.Files. In the first place, the java.nio.file.Files class deals only with static methods.

It is said that it is safer to use 2. for Windows. I will explain the details from now on.

java.io.File.canWrite Again, this is an instance method.

Below is a sample.

sample.kt


val file = File("Arbitrary file path")
val canWrite = file.canWrite() //True if writable

Very easy.

This only looks to see if the file has the writable attribute. For Linux, so-called Linux permissions, and for Windows, the read-only attribute of file properties.

In other words, it is not possible to check whether the running Java process has authority to the OS. More specifically, ** It doesn't support Windows UAC permission check. ** **

Therefore, if the directory cannot be written without OS administrator privileges such as directly under the C drive, even if canWrite is true, ʻIOException` will be thrown mercilessly when writing the file, which is a failure (Java process). Of course, this is not the case if is running with administrator privileges)

java.nio.file.Files.isWritable That's where this method comes in. How to write is like this.

sample.kt


val file = File("Arbitrary file path")
val canWrite = Files.isWritable(file.toPath()) //True if writable

This will check if the Java process has writable permissions ** as well. The following is a quote from the Oracle JDK documentation. https://docs.oracle.com/javase/jp/8/docs/api/java/nio/file/Files.html#isWritable-java.nio.file.Path-

This method verifies that the file exists and ** this Java virtual machine has the appropriate privileges to allow the file to be opened for writing **.

So, here you can check including UAC. By the way, read permission and execute permission can be checked in the same way with methods such as ʻisReadable and ʻisExecutable.

that's all

As far as I checked, there were only articles related to UAC that were troubled by the installer in the Vista era, and there was really no information other than the Oracle reference. Isn't it unexpectedly stuck here? Or are there few examples of native apps in the first place? ??

I hope it helps people who are stuck with the same event.

Recommended Posts

Check with Java / Kotlin that files cannot be written in UAC on Windows
[Solution] Java cannot be installed on Windows 10 + ATOK 2017
Install Java with zip on Windows
Java cannot be installed on Ubuntu 13.04
Note that system properties including JAXBContext cannot be used in Java11
A story about a Spring Boot project written in Java that supports Kotlin
Using JupyterLab + Java with WSL on Windows 10
Java code that cannot be used from Kotlin (for in-house study sessions)
[Kotlin] Delete files with duplicate contents [Java]
[Java] Scenes that cannot be reassigned (substantially final)
A bat file that uses Java in windows
Compiled kotlin with cli with docker and created an environment that can be executed with java
Check coverage with Codecov in Java + Gradle + Wercker configuration
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Use Java Web Start in an OpenJDK environment on Windows
Four-in-a-row with gravity that can be played on the console
Java (super beginner edition) that can be understood in 180 seconds
Zip compression with Java in Windows environment without garbled characters