forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNIOFileChannel01.java
More file actions
106 lines (83 loc) · 3.3 KB
/
Copy pathNIOFileChannel01.java
File metadata and controls
106 lines (83 loc) · 3.3 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.learnjava.io.nio;
import java.awt.print.Pageable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author LuoHaiYang
*/
public class NIOFileChannel01 {
public static void main(String[] args) throws Exception {
// write to disk
// write("hello nio");
//read("/Users/tianbao/file01.log");
//readAndWrite("/Users/tianbao/file01.log", "/Users/tianbao/file02.log");
useTransferFrom("/Users/tianbao/file03.log", "/Users/tianbao/file02.log");
}
public static void useTransferFrom(String toPath, String fromPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(fromPath);
FileOutputStream fileOutputStream = new FileOutputStream(toPath);
FileChannel sourceCh = fileInputStream.getChannel();
FileChannel destCh = fileOutputStream.getChannel();
// use transferFrom to copy
destCh.transferFrom(sourceCh, 0, sourceCh.size());
// clouse
sourceCh.close();
destCh.close();
fileInputStream.close();
fileOutputStream.close();
}
// read and write in common buffer
public static void readAndWrite(String fromPath, String toPath) throws Exception {
FileInputStream fileInputStream = new FileInputStream(fromPath);
FileChannel fileChannel01 = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(toPath);
FileChannel fileChannel02 = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(512);
while (true) {
byteBuffer.clear();
// disk -> channel -> bytebuffer
int read = fileChannel01.read(byteBuffer);
System.out.println("read = " + read);
if (read == -1) {
break;
}
// change index
byteBuffer.flip();
// bytebuffer -> channel -> disk
fileChannel02.write(byteBuffer);
}
fileInputStream.close();
fileOutputStream.close();
}
public static void read(String path) throws Exception {
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel();
// create buffer
ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
// disk -> channel -> buffer
fileChannel.read(byteBuffer);
// buffer -> jvm
System.out.println(new String(byteBuffer.array()));
// disk -> channel -> buffer -> jvm
fileChannel.close();
fileInputStream.close();
}
public static void write(String str) throws Exception {
File file;
// jvm -> output -> disk
FileOutputStream fileOutputStream = new FileOutputStream("/Users/tianbao/file01.log");
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// set str into bytebuffer
byteBuffer.put(str.getBytes());
byteBuffer.flip();
// write to channel
fileChannel.write(byteBuffer);
fileOutputStream.close();
// jvm -> buffer -> channel -> disk
}
}