From the course: Kotlin Essential Training: Functions, Collections, and I/O
Unlock the full course today
Join today to access over 24,700 courses taught by industry experts.
Read and write to files - Kotlin Tutorial
From the course: Kotlin Essential Training: Functions, Collections, and I/O
Read and write to files
- [Instructor] How do you read a list of test scores from a file or save processed data to a file for later use? Let's explore how we can work with file inputs and outputs in Kotlin. Let's start with reading from files. To get started, we'll create a new text file in our root project called scores.txt. So to do that from IntelliJ, I'll open up the project directory window here. And within the root project directory, I'm going to right click, go to new and file. And I'm going to name this file scores.txt and hit enter . In that file, we'll add a list of data representing user IDs and test scores separated by a colon. So I'm just going to paste in some example data here. So here we have seven pairs of ID and test scores. So now back within our main.kt file, we want to read that file in and store it as a variable. So to do so, we will create a variable called scoresFile and then we're going to create an instance of the file.…