有Kotlin的setReadOnly()吗?

时间:2019-12-21 21:00:44

标签: kotlin

Kotlin中的文件是否具有等效的Java函数<span>test, </span><span>test, </span><span>test, </span>

1 个答案:

答案 0 :(得分:3)

Java和Kotlin可以互操作而不会出现问题(即you can call Java from Kotlinvice versa),因此,通过包含以下内容,您仍然可以在任何Kotlin类中仅使用setReadOnly()函数分别对java.io.*java.io.File的适当引用:

// Notice the Java reference in this Kotlin class
import java.io.*

object ExampleKotlinProgram {

@JvmStatic
fun main(args: Array<String>) {
    // Get your file (File is exposed through Java's java.io reference above)
    val file = File("C:\\your-file.txt")

    // Use the setReadOnly() call to check if it can be access or set to read only
    if (f.setReadOnly()) {
        // Indicate it could be set to read only
        println("File set as Read Only")
    } else {
        // Indicate it couldn't be set to read only
        println("File cannot be set as Read Only")
    }
}

}