Why Kotlin's loop (for) not work correct with

2019-09-21 13:12发布

问题:

In my Kotlin project in folder src/resources/ I has file pairs_ids.txt.

Here code: This is a property file:

key=value

The count of all lines are 1389.

Here code that read content of this file line by line.

import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger("Exchange")

 private var allSymbolsIDsMap: Map<String, String> = mapOf()    
 val pairsIDs = getResourceAsText("/pairs_ids.txt")
 allSymbolsIDsMap = pairsIDs.split(",").associate {
 val (left, right) = it.split("=")
          left to right.toString()
 }
 logger.info("allSymbolsIDsMap_size = " + allSymbolsIDsMap.size)
 var countAllSymbolHandler = 0
 for ((key, value) in allSymbolsIDsMap) {
   countAllSymbolHandler++
   logger.info("countAllSymbolHandler = $countAllSymbolHandler")
}

private fun getResourceAsText(path: String): String {
    return object {}.javaClass.getResourceAsStream(path).bufferedReader().use { it.readText() }
}

Result:

Start project:

allSymbolsIDsMap_size = 1389
Start project - "countAllSymbolHandler =" print 1113 times

Again start project:

allSymbolsIDsMap_size = 1389
"countAllSymbolHandler =" print 242 times

If replace logger.info by simple println then success work. The count is always 1389.

Why loop (for) not work correct with logger?

回答1:

Try:

override fun run(configuration: AppConfig?, environment: Environment?) {
        val logger = LoggerFactory.getLogger(this::class.java)
        javaClass.getResourceAsStream("/pairs_ids.txt").bufferedReader().use { reader -> reader.readLines() }.forEach { line -> logger.info(line) }
    }

When playing with input/output streams, use Kotlin's use extension methods, and do all of your processing inside of the use block.

This will handle all opening and closing of the streams so that there are no leaks, or forgetting to close/flush etc.



标签: kotlin slf4j