[英]Bufferedreader.readline() cause infinite loop
我想读取文本文件,但无限循环总是出现在 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()
}
如果您只想逐行处理文件,则在 while 循环结束之前添加 strLine = br.readLine() 。 请记住为您的 while 循环添加 { } 因为块中将有 1 个以上的语句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.