我要列出文件夹内的所有文件.只有两个文件,a. js和. json
这是我的代码,但它不工作,每次我点击控制台打印No files found or directory does not exist
.我不知道文件的文件夹是否必须在其他位置或问题的路径.我遵循这example
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ReadFilesTheme {
// A surface container using the 'background' color from the theme
Surface(
color = MaterialTheme.colorScheme.background
) {
Column {
ReadJSFilesButton()
}
}
}
}
}
}
@Composable
fun ReadJSFilesButton() {
Button(
onClick = { read() }
) {
Text(text = "get files")
}
}
fun read() {
val directoryPath = "com/example/readfiles/files"
val directory = File(directoryPath)
val files = directory.listFiles()?.filter { it.isFile }
files?.forEach { file ->
println(file.name)
} ?: println("No files found or directory does not exist.")
}