如何防止Line Reader跳过第一行?

问题描述 投票:0回答:1

我正在尝试从文本文件中读取行。现在我正在计算行数,它正在跳过第一行。我认为if (line == null) break周围有问题。我很确定我错过了一些东西。所以请看一下我的代码。

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (data == null)
            return
        if (requestCode==requestcode) {
            val filepath=data.data
            file = filepath.toString()
            val cursor=contentResolver.openInputStream(android.net.Uri.parse(filepath.toString()))
            lbl.text=filepath.toString()
            master_path=filepath.toString()
            noti=cursor.toString()
            val db=this.openOrCreateDatabase("database.db", Context.MODE_PRIVATE, null)
            val tableName="Master"
            db.execSQL("delete from $tableName")
            val text =  StringBuilder()
            try {
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        val file=InputStreamReader(cursor)
                        val buffer=BufferedReader(file)
                        buffer.readLine()
                        var lineCount = 0
                        db.beginTransaction()

                        while(true) {
                            val line=buffer.readLine()

                            if (line == null) break //Something is wrong here I Think
                            lineCount++

                        }
                        println(lineCount.toString())
                        line = lineCount
                        db.setTransactionSuccessful()
                        db.endTransaction()
                    } catch (e: IOException) {
                        if (db.inTransaction())
                            db.endTransaction()
                        val d=Dialog(this)
                        d.setTitle(e.message.toString() + "first")
                        d.show()
                    }

                } else {
                    if (db.inTransaction())
                        db.endTransaction()
                    val d=Dialog(this)
                    d.setTitle("Only CSV files allowed")
                    d.show()
                }
            } catch (ex: Exception) {
                if (db.inTransaction())
                    db.endTransaction()

                val d=Dialog(this)
                d.setTitle(ex.message.toString() + "second")
                d.show()
            }

        }

    }
android file kotlin bufferedreader
1个回答
0
投票

只需在buffer.readline()之前删除while(true),就可以使用

© www.soinside.com 2019 - 2024. All rights reserved.