-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetData.java
More file actions
62 lines (49 loc) · 1.53 KB
/
Copy pathGetData.java
File metadata and controls
62 lines (49 loc) · 1.53 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
package Chapter18_IO;
import java.nio.ByteBuffer;
import static Fourth_util.Print.print;
import static Fourth_util.Print.printnb;
/**
* 在 Buffer 中操作基本类型
*/
public class GetData {
private static final int SIZE = 1024;
public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.allocate(SIZE);
// 一次 get 一个字节
int i = 0;
while (i++ < bb.limit()) {
if (bb.get() != 0)
print("nonzero");
}
print("i = " + i);
// get 基本数据类型
bb.rewind();
System.out.println("Store and read a char array:");
bb.asCharBuffer().put("Howdy!");
char c;
while ((c = bb.getChar()) != 0)
printnb(c + " ");
print();
bb.rewind();
System.out.println("Store and read a short:");
bb.asShortBuffer().put((short) 471142);
print(bb.getShort());
bb.rewind();
System.out.println("Store and read an int:");
bb.asIntBuffer().put(99471142);
print(bb.getInt());
bb.rewind();
System.out.println("Store and read a long:");
bb.asLongBuffer().put(99471142);
print(bb.getLong());
bb.rewind();
System.out.println("Store and read a float:");
bb.asFloatBuffer().put(99471142);
print(bb.getFloat());
bb.rewind();
System.out.println("Store and read a double:");
bb.asDoubleBuffer().put(99471142);
print(bb.getDouble());
bb.rewind();
}
}