Marlock
Marlock

Reputation: 285

Problem with getting rights data [for execution / read / write] file

I'm writing a program that should output a meta-info (size, permissions to execute / read / write, time of last modification) of all files from the specified directory. I received information about all the information, except the rights to execute / read / write.

I tried to get this info using PosixFilePermissions, but when added to the List I get Exception in thread "main" java.lang.UnsupportedOperationException. Maybe you should use some other library? Or did I make a mistake somewhere? I would be grateful for any advice!

fun long(path:Path) : MutableList<String> {
        var listOfFiles = mutableListOf<String>()
        val files = File("$path").listFiles()
        var attr: BasicFileAttributes 
        Arrays.sort(files, NameFileComparator.NAME_COMPARATOR)
        files.forEach {
            if (it.isFile) {
                attr = Files.readAttributes<BasicFileAttributes>(it.toPath(), BasicFileAttributes::class.java)
                listOfFiles.add("${it.name} ${attr.size()} ${attr.lastModifiedTime()}" +
                        " ${PosixFilePermissions.toString(Files.getPosixFilePermissions(it.toPath()))}")
            }
            else listOfFiles.add("dir ${it.name}")
        }
        return listOfFiles
    }

Upvotes: 1

Views: 173

Answers (1)

Lunivore
Lunivore

Reputation: 17657

PosixFilePermissions are only usable for POSIX-compatible file systems (Linux etc.).

For a Windows system, the permissions have to be accessed directly:

file.canRead()
file.canWrite()
file.canExecute()

Upvotes: 1

Related Questions