-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkWithFile.java
More file actions
37 lines (31 loc) · 1.07 KB
/
WorkWithFile.java
File metadata and controls
37 lines (31 loc) · 1.07 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
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class WorkWithFile {
public static CharBuffer read(String pathSource) {
CharBuffer ch = null;
try (RandomAccessFile raf = new RandomAccessFile(pathSource, "rw");
FileChannel fc = raf.getChannel()) {
ByteBuffer bb = ByteBuffer.allocate((int) fc.size());
fc.read(bb);
bb.flip();
Charset charset = StandardCharsets.UTF_8;
ch = charset.decode(bb);
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
public static void write(String pathDestination, StringBuilder st) throws IOException {
Path outputPath = Paths.get(pathDestination);
Files.createFile(outputPath);
Files.writeString(outputPath, st);
}
}