Files
ITMO/client/src/main/kotlin/io/FileIO.kt
T
LeterZP 1d57fb83a4 Squashed 'Programming/ProgLab6/' content from commit 3e98b8e
git-subtree-dir: Programming/ProgLab6
git-subtree-split: 3e98b8e205b1a576c861f90028b198d5052a79c3
2026-05-17 15:29:12 +03:00

50 lines
1.5 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package io
import java.io.BufferedInputStream
import java.io.BufferedWriter
import java.io.FileInputStream
import java.io.FileWriter
/**
* Класс для чтения данных из файла.
*
* @param file Имя файла типа [String].
*
* @constructor Создаёт готовый к использованию объект, принимая все описанные выше параметры.
*
* @since 1.0
*/
class FileIO(private val file: String) {
/**
* Читает весь файл.
*
* @return [String] строку файла.
*
* @throws [NoSuchElementException] В случае, если в файле не осталось непрочитанных строк.
* @throws [java.io.IOException] В случае, если файла не существует.
*
* @since 1.0
*/
fun readFile(): String {
val reader = BufferedInputStream(FileInputStream(file))
val text = reader.readAllBytes().decodeToString()
reader.close()
return text
}
/**
* Записывает строки в файл.
*
* @param text Строки для записи типа [String].
*
* @throws [java.io.IOException] В случае, если файла не существует.
*
* @since 1.0
*/
fun writeToFile(text: String) {
val writer = BufferedWriter(FileWriter(file))
writer.write(text)
writer.close()
}
}