Giorgi
09/01/2023, 4:42 PMGiorgi
09/01/2023, 4:43 PMactual suspend fun MPFile<Any>.toDomainModel(): HistoryFile {
val selectedFile = (platformFile as NSURL)
var fileContents: String = ""
memScoped {
val file = fopen(selectedFile.path, "r")
if (file != null) {
val bufferSize = 1024
val buffer = ByteArray(bufferSize)
while (true) {
val bytesRead = fread(buffer.refTo(0), 1.convert(), bufferSize.convert(), file)
if (bytesRead <= 0u) break
val contentChunk = buffer.copyOfRange(0, bytesRead.toInt())
fileContents += contentChunk.decodeToString()
}
fclose(file)
} else {
println("Failed to open the file.")
}
}
return HistoryFile(path, fileContents)
}
But it is very slow. On some websites I found this Swift code
let file = "/Users/user/Documents/text.txt"
let path=URL(fileURLWithPath: file)
let text=try? String(contentsOf: path)
How can I convert it to Kotlin?mbonnin
09/01/2023, 6:52 PMmbonnin
09/01/2023, 6:53 PMFileSystem.SYSTEM.openReadOnly("path/to/file.txt".toPath()).source().buffer().readUtf8()
Giorgi
09/05/2023, 4:16 PM