在 Android Studio Kotlin 中读取文本文件并制作指针

Reading a text file in Android Studio Kotlin and making pointers

我是 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()
        }
    }
}

获得指示后,我会在回收站视图中加载信息,这样您就可以select想要哪个游戏(信息可见)。

我已经爬取了关于此的堆栈溢出,但是大多数问题只是阅读整个文本文件 return,而不是将指针放入参考位置

所以我想出了 'issues' 这方面的问题。 raf.readLine() 非常慢。这是一个已知问题,但并没有真正谈论太多。无论如何,我现在使用 .readFully(),速度要快得多。