-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathTask05Main.java
More file actions
42 lines (35 loc) · 1.32 KB
/
Task05Main.java
File metadata and controls
42 lines (35 loc) · 1.32 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
package com.example.task05;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Task05Main {
public static void main(String[] args) {
String pathToFile = args[0]; // "/home/user/file.txt"
try
{
String s = readFile(pathToFile);
System.out.println(s);
} catch (FileNotFoundException ex)
{
String message = String.format("файл \"%s\" не найден\n", pathToFile);
System.out.print(message);
} catch (IOException ex)
{
String message = String.format("произошла ошибка при чтении файла \"%s\"\n", pathToFile);
System.out.print(message);
}
}
public static String readFile(String pathToFile) throws IOException {
FileReader fileReader = new FileReader(pathToFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder stringBuilder = new StringBuilder();
String currentLine;
while ((currentLine = bufferedReader.readLine()) != null) {
stringBuilder.append(currentLine);
stringBuilder.append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
}