1

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?

Rafa
  • 3,219
  • 4
  • 38
  • 70
  • "I'm creating them by reading from a usb" -- you do not have access to arbitrary files on [removable storage in Android](https://commonsware.com/blog/2017/11/15/storage-situation-removable-storage.html). – CommonsWare Apr 15 '19 at 21:30

1 Answers1

1

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.

Doc
  • 10,831
  • 3
  • 39
  • 63
  • based on a few different examples on renaming files online, that should work `https://stackoverflow.com/questions/1158777/rename-a-file-using-java` Why would it affect being able to set it as readable as well? – Rafa Apr 15 '19 at 18:55
  • 1
    you are not checking for `Hello2.txt` existance – Doc Apr 15 '19 at 18:58
  • 2
    and setReadable is dependent on system for permissions – Doc Apr 15 '19 at 18:59