'Reading a text file in Android Studio Kotlin and making pointers
I am fairly new to Android + Kotlin, however I am wondering if there is a faster way to read a text (.pgn) file and mark pointers to places in the file for later reference.
At the moment I am using RandomAccessFile however it is incredible slow for a process that should be extremely quick.
This is my code at the moment:
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()
}
}
}
Once I have the pointers, I then load the information in a recycler view so as you're able to select which game (with information visible) you wish.
I have crawled stack overflow regarding this, however the majority of questions are just to read a whole text file and return that, not put pointers into places for reference
Solution 1:[1]
So I managed to figure out the 'issues' regarding this. raf.readLine()
is EXTREMELY slow. This is a known problem, but not really talked about so much. Anyway I now use .readFully()
, which is substantially faster.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tikhedewal |