-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.java
More file actions
60 lines (55 loc) · 2.28 KB
/
FileHandler.java
File metadata and controls
60 lines (55 loc) · 2.28 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.*;
import java.util.Scanner;
class FileHandler {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(true){
System.out.println("Выберите действие: ");
System.out.println("1. Чтение текста из файла");
System.out.println("2. Запись текста в файл");
System.out.println("3. Завершить программу");
int chooseInput = scanner.nextInt();
scanner.nextLine();
switch (chooseInput){
case 1:
readFromFile(scanner);
break;
case 2:
writeToFile(scanner);
break;
case 3:
System.out.println("Программа завершена");
return;
default:
System.err.println("Неверный выбор, попробуйте еще раз");
}
}
}
public static void readFromFile(Scanner scanner){
System.out.println("Введите имя файла для чтения");
String nameFile = scanner.nextLine();
try(BufferedReader reader = new BufferedReader(new FileReader(nameFile))){
String line;
System.out.println("Содержимое файла");
while ((line = reader.readLine()) != null){
System.out.println(line);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void writeToFile(Scanner scanner){
System.out.println("Введите имя файла для записи");
String fileName = scanner.nextLine();
System.out.println("Введите текст для файла");
String fileText = scanner.nextLine();
try(BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))){
writer.write(fileText);
System.out.println("Текст был успешно записан!");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}