Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **Reading a textfile**
- ```kotlin
- val lines = File("someFile.txt").readLines()
- for (line in lines) {
- println(line)
- }
- ```
- **Reading a textfile using a buffered stream**
- ```kotlin
- val lines = File("hello.txt").bufferedReader()
- lines.forEachLine {
- println(it)
- }
- ```
- **Reading in objects from a file**
- ```kotlin
- fun main(args: Array<String>) {
- val reader = File("orders.csv").bufferedReader()
- reader.readLines()
- .drop(1) //drop headers
- .map { it.split(",") }
- .map {
- CustomerOrder(
- customerId = it[0].toInt(),
- customerOrderId = it[1].toInt(),
- orderDate = LocalDate.parse(it[2]),
- productId = it[3].toInt(),
- quantity = it[4].toInt()
- )
- }
- }
- data class CustomerOrder(
- val customerOrderId: Int,
- val customerId: Int,
- val orderDate: LocalDate,
- val productId: Int,
- val quantity: Int
- )
- ```
Add Comment
Please, Sign In to add comment