我对Android+Kotlin相当陌生,但是我想知道是否有更快的方法来读取文本(.pgn)文件并标记指向文件中位置的指针以供以后参考。
目前,我使用的是RandomAccessFile,但是对于一个应该非常快的进程来说,它的速度非常慢。
这是我目前的代码:
private fun loadPGN() {
try {
val selectedPGN = File((context as MainActivity).filesDir, "mygames.pgn")
val raf = RandomAccessFile(selectedPGN, "r")
val length = raf.length()
raf.seek(0)
charPosition = raf.filePointer
while (charPosition < length) {
val str : String = raf.readLine()
if (str.contains("[Event ")) {
mutableListEvent += charPosition
findMoves = true
}
if (findMoves && ((str.startsWith(str.filter { it.isDigit() }) && !str.startsWith("[")) || str.startsWith("{ "))) {
mutableListMoves += charPosition
findMoves = false
}
charPosition = raf.filePointer
}
for (i in 0 until mutableListEvent.size) {
val event = if (mutableListEvent[i] != mutableListEvent[mutableListEvent.size - 1]) mutableListEvent[i + 1] else length
val moves = mutableListMoves[i]
raf.seek(mutableListEvent[i])
eventStr = raf.readLine().removeRange(0,8).replace("\"]", "")
eventMutableList.add(eventStr)
difference += (event - moves)
headerLength += (mutableListMoves[i] - mutableListEvent[i])
raf.seek(moves)
val byteArray = ByteArray(difference[i].toInt())
raf.readFully(byteArray)
val string = String(byteArray)
var stringEdited = String(byteArray).replace("\n","")
if (stringEdited.contains("{[")) {
val re = "\\{\\[.*?]}".toRegex()
stringEdited = re.replace(stringEdited,"")
}
gamePlayMutableList.add(string)
}
// Header Information
for (i in 0 until headerLength.size) {
raf.seek(mutableListEvent[i])
charPosition = raf.filePointer
while (charPosition < mutableListMoves[i]) {
val str = raf.readLine()
if (str.contains("[Site \"") || str.contains("[Date \"") || str.contains("[Round \"") || str.contains("[White \"") || str.contains(
"[Black \""
) || str.contains("[Result \"") || str.contains("[EventDate \"") || str.contains("[PlyCount \"")
) {
if (str.contains("[Site \"")) {
siteMutableList += str.replace("[Site \"", "").replace("\"]", "")
}
if (str.contains("[Date \"")) {
dateMutableList += str.replace("[Date \"", "").replace("\"]", "")
}
if (str.contains("[Round \"")) {
roundMutableList += str.replace("[Round \"", "").replace("\"]", "")
}
if (str.contains("[White \"")) {
whiteMutableList += str.replace("[White \"", "").replace("\"]", "")
}
if (str.contains("[Black \"")) {
blackMutableList += str.replace("[Black \"", "").replace("\"]", "")
}
if (str.contains("[Result \"")) {
resultMutableList += str.replace("[Result \"", "").replace("\"]", "")
}
if (str.contains("[EventDate \"")) {
eventDateMutableList += str
}
if (str.contains("[PlyCount \"")) {
plyCountMutableList += str
}
}
charPosition = raf.filePointer
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}
一旦有了指针,我就会将信息加载到回收器视图中,以便您能够选择您想要的游戏(信息可见)。
关于这一点,我已经对堆栈溢出进行了爬网,但是大多数问题只是读取整个文本文件并返回,而不是将指针放在参考位置