1

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
    }
Marlock
  • 275
  • 2
  • 9
  • Are you working with a POSIX file system? Otherwise this similar question might shed some light: https://stackoverflow.com/questions/14415960/java-lang-unsupportedoperationexception-posixpermissions-not-supported-as-in – Lunivore Apr 14 '19 at 00:13
  • I work with Windows OS, I understood what the error was, but how to get information about rights in the Windows file system? – Marlock Apr 14 '19 at 00:36
  • Possible duplicate of [java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute on Windows](https://stackoverflow.com/questions/14415960/java-lang-unsupportedoperationexception-posixpermissions-not-supported-as-in) – Erwin Bolwidt Apr 14 '19 at 10:26
  • @Marlock Why don't you put that in your question? Please use the [edit] link to update it. – Erwin Bolwidt Apr 14 '19 at 10:27
  • To the voters to close: I chose to answer this and keep it open because the "possible duplicate" @Marlock mentioned doesn't actually show the permission getters, only the setters, and it took me a bit of reading docs to find out that they started with "can" instead of "is". Hopefully saves the next person a few minutes. – Lunivore Apr 14 '19 at 19:23

1 Answers1

1

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()
Lunivore
  • 17,277
  • 4
  • 47
  • 92