niel
niel

Reputation: 125

Bufferedreader.readline() cause infinite loop

I want to read the text file but the infinite loop always appears on strLine.split.... I got my expected value of array, "6". But when I created a new br.readline before the strLine.startsWith the infinite loop does not shown anymore but the array value is "2". I need to get the "6" to run some code inside the condition.

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

Upvotes: 1

Views: 235

Answers (1)

user11275695
user11275695

Reputation: 26

If all you want to do is process the file line by line, then add a strLine = br.readLine() just before the end of the while loop. Remember to add { } for your while loop because there will be more than 1 statement in the block

Upvotes: 1

Related Questions