我想读取文本文件,但是无限循环始终出现在strLine.split ....上。我得到了数组“ 6”的期望值。但是,当我在strLine.startsWith无限循环之前创建新的br.readline时,不再显示,但数组值为“ 2”。我需要获取“ 6”才能在条件中运行一些代码。
try {
val file = File(Environment.getExternalStorageDirectory().toString() + "/drawings/$fileName.txt")
Timber.d("FILENAME -----> ${file.exists()}")
val fStream = FileInputStream(file)
Timber.d("FSTREAM -----> ${fStream == null}")
val dataInput = DataInputStream(fStream)
Timber.d("DATAINPUT -----> ${dataInput == null}")
val br = BufferedReader(InputStreamReader(dataInput))
val strLine = br.readLine()
Timber.d("STRLINE -----> $strLine")
var strData: Array<String>
var colorIndex: Int
var sizeIndex: Int
// Close the input stream
while ((strLine) != null)
if (strLine.startsWith("START")) {
// val strLine = br.readLine()
strData = strLine.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
Timber.d("STRDATA ---> ${strData.size}")
if (strData.size == 6) {
colorIndex = Integer.parseInt(strData[2])
sizeIndex = Integer.parseInt(strData[5])
when (Integer.parseInt(strData[1])) {
1 -> {
action = EditAction.PEN
when (colorIndex) {
0 -> this.color = Color.GREEN
1 -> this.color = android.graphics.Color.rgb(255, 192, 203) // PINK
2 -> this.color = Color.YELLOW
3 -> this.color = Color.BLUE
4 -> this.color = Color.BLACK
}
when (sizeIndex) {
0 -> this.size = Size.SIZE_1
1 -> this.size = Size.SIZE_2
2 -> this.size = Size.SIZE_3
3 -> this.size = Size.SIZE_4
4 -> this.size = Size.SIZE_5
}
}
}
touchStart(parseFloat(strData[3]),
parseFloat(strData[4]))
} else {
return
}
} else {
strData = strLine.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
if (strData.size == 2) {
touchMove(parseFloat(strData[0]),
parseFloat(strData[1]))
}
}
dataInput.close()
} catch (e: FileNotFoundException) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
答案 0 :(得分:1)
如果您要做的是逐行处理文件,则在while循环结束之前添加strLine = br.readLine()。请记住为while循环添加{},因为在该块中将有1条以上的语句