Rafa
Rafa

Reputation: 3349

Can't change Java file permissions

I'm working with Files in Kotlin and Java and can't change the permission are do anything with them at all really. I'm thinking it's from a lack of understanding of how they work.

fun main(args: Array<String>) {
   val file1 = File("Hello.txt")
   val file2 = File("Hello2.txt")

   if (file1.renameTo(file2)) {
       println("rename succeeded")
   } else {
       println("rename failed")
   }

   if(file1.setReadable(true, true)) {
       println("readable succeeded")
   } else {
    println("readable failed")
   }
}

I create the files and it fails both when I try to rename, and when I try to set to readable.

I have a much larger project where I'm creating them by reading from a usb, saving them to a temporary location, and attempting to do the same thing. Figured I'd start with the smaller more easily reproducible example.

Is there anything I have to configure to make both of these files readable?

Upvotes: 1

Views: 180

Answers (1)

Doc
Doc

Reputation: 11651

If a file named Hello2.txt is already present in the location then you won't be able to rename Hello.txt to Hello2.txt.

The first line of code for rename works if there is no file name conflict.

Upvotes: 1

Related Questions