-
Notifications
You must be signed in to change notification settings - Fork 1
/
Files.java
38 lines (34 loc) · 1.17 KB
/
Files.java
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
package com.yurii.salimov.lesson12.task01;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class Files {
public static void write(final String fileName, final Object obj) {
try (FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter output = new BufferedWriter(fileWriter)) {
output.write(obj.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static String read(final String fileName) throws FileNotFoundException {
final StringBuilder sb = new StringBuilder();
try (FileReader fileReader = new FileReader(fileName);
BufferedReader input = new BufferedReader(fileReader)) {
String str;
while ((str = input.readLine()) != null) {
sb.append(str).append("\r\n");
}
} catch (IOException ex) {
ex.printStackTrace();
}
return sb.toString();
}
}