Guest User

Untitled

a guest
Feb 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. **Reading a textfile**
  2. ```kotlin
  3. val lines = File("someFile.txt").readLines()
  4.  
  5. for (line in lines) {
  6. println(line)
  7. }
  8. ```
  9. **Reading a textfile using a buffered stream**
  10. ```kotlin
  11. val lines = File("hello.txt").bufferedReader()
  12.  
  13. lines.forEachLine {
  14. println(it)
  15. }
  16. ```
  17.  
  18. **Reading in objects from a file**
  19. ```kotlin
  20. fun main(args: Array<String>) {
  21. val reader = File("orders.csv").bufferedReader()
  22.  
  23. reader.readLines()
  24. .drop(1) //drop headers
  25. .map { it.split(",") }
  26. .map {
  27. CustomerOrder(
  28. customerId = it[0].toInt(),
  29. customerOrderId = it[1].toInt(),
  30. orderDate = LocalDate.parse(it[2]),
  31. productId = it[3].toInt(),
  32. quantity = it[4].toInt()
  33. )
  34. }
  35. }
  36.  
  37. data class CustomerOrder(
  38. val customerOrderId: Int,
  39. val customerId: Int,
  40. val orderDate: LocalDate,
  41. val productId: Int,
  42. val quantity: Int
  43. )
  44. ```
Add Comment
Please, Sign In to add comment