-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileAccess.kt
37 lines (33 loc) · 1.12 KB
/
FileAccess.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ticketmachine.software
import java.io.BufferedReader
import java.io.FileReader
import java.io.PrintWriter
// Function to read from a specified file and return data as an Array of Strings
fun readFile(fileName: String): Array<String> {
// Creates a reader
val reader = BufferedReader(FileReader(fileName))
// Creates an array to store all data lines
var dataArray: Array<String> = emptyArray()
// Creates a mutable variable to store the current line that bufferedReader is reading
var currentLine = reader.readLine()
while (currentLine != null) {
// Adds data line to the array
dataArray += currentLine
// Moves to next data line
currentLine = reader.readLine()
}
// Closes reader
reader.close()
return dataArray
}
// Function to write given data as an Array of Strings to a specified file
fun writeFile(fileName: String, dataArray: Array<String>) {
// Creates a writer
val writer = PrintWriter(fileName)
for (data in dataArray) {
// Writes the current data line
writer.println(data)
}
// Closes writer
writer.close()
}