Kotlin中的文件是否具有等效的Java函数<span>test, </span><span>test, </span><span>test, </span>
?
答案 0 :(得分:3)
Java和Kotlin可以互操作而不会出现问题(即you can call Java from Kotlin和vice 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")
}
}
}