Buffer
Exam.java:
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
public class Exam {
public static void main(String[] args) {
// 創建緩衝區的第一種方式
CharBuffer cBuf = CharBuffer.allocate(10);
cBuf.put('A');
cBuf.put('B');
cBuf.put('C');
cBuf.put('D');
cBuf.put('E');
cBuf.put('F');
cBuf.put('G');
cBuf.put('H');
// 迴繞
cBuf.rewind();
while (cBuf.hasRemaining()) {
System.out.print(cBuf.get());
}
System.out.println();
// 創建緩衝區的第二種方式
byte[] bytes = new byte[15];
for (int i = 0; i < 15; i++) {
bytes[i] = (byte) ('A' + i);
}
ByteBuffer buf = ByteBuffer.wrap(bytes);
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
}
}
MappedChannelRead.java:
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MappedChannelRead {
public static void main(String[] args) {
// 用try-with-resources的方式獲取文件的通道
try (FileChannel fChan = (FileChannel) Files.newByteChannel(Paths.get("test.txt"))) {
// 獲取文件大小
long fSize = fChan.size();
// 現在,將文件映射到緩衝區。
MappedByteBuffer mBuf = fChan.map(FileChannel.MapMode.READ_ONLY,0,fSize);
// 從緩衝區讀取和顯示字節。
for(int i = 0; i < fSize; i++){
System.out.print((char) mBuf.get());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
MappedChannelWrite.java:
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class MappedChannelWrite {
public static void main(String[] args) {
try (FileChannel fChan = (FileChannel) Files.newByteChannel(Paths.get("test.txt"),
StandardOpenOption.WRITE,
StandardOpenOption.READ,
StandardOpenOption.CREATE)) {
// 然後將文件映射到緩存區中
MappedByteBuffer mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26);
// 將一些字節寫入緩衝區
for (int i = 0; i < 26; i++) {
mBuf.put((byte) ('A' + i));
}
// 寫入文件
mBuf.force();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Channel
Exam2.java:
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
public class Exam2 {
public static void main(String[] args) throws IOException {
RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(10);
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
buf.flip();
while(buf.hasRemaining()){
System.out.print((char) buf.get());
}
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
}
}
ExplicitChannelWrite.java:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class ExplicitChannelWrite {
public static void main(String[] args) {
try (FileChannel fChan = (FileChannel) Files.newByteChannel(Paths.get("test.txt"),
StandardOpenOption.WRITE,
StandardOpenOption.CREATE)) {
// 創建一個緩衝區
ByteBuffer mBuf = ByteBuffer.allocate(26);
// 向緩衝區中寫入一些字節
for (int i = 0; i < 26; i++) {
mBuf.put((byte) ('A' + i));
}
// 重置緩衝區,以讓其寫入
mBuf.rewind();
// 將緩衝區寫入輸出文件
fChan.write(mBuf);
} catch (IOException e) {
e.printStackTrace();
}
}
}
ExplicitChannelRead.java:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
public class ExplicitChannelRead {
public static void main(String[] args) {
int count;
try (SeekableByteChannel fChan = Files.newByteChannel(Paths.get("test.txt"))) {
// 分配一個緩衝區
ByteBuffer mBuf = ByteBuffer.allocate(128);
do {
// 每次調用read()時,將來自文件的數據填充mBuf指定的緩衝區
count = fChan.read(mBuf);
// 到達文件結尾時停止
if (count != -1) {
// 迴繞緩衝區以便讀取
mBuf.rewind();
// 從緩衝區中讀取字節,並在屏幕上顯示爲字符。
for (int i = 0; i < count; i++) {
System.out.print((char) mBuf.get());
}
}
} while (count != -1);
} catch (InvalidPathException e) {
System.out.println("Path Error " + e);
} catch (IOException e) {
System.out.println("I/O Error " + e);
}
}
}
Scatter/Gather
Exam3.java:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Exam3 {
public static void main(String[] args) throws IOException {
RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(5);
ByteBuffer buf2 = ByteBuffer.allocate(5);
ByteBuffer[] bufferArray = {buf, buf2};
// Scatter
long bytesRead = inChannel.read(bufferArray);
while (bytesRead != -1) {
buf.flip();
buf2.flip();
while(buf.hasRemaining()){
System.out.print((char) buf.get());
}
while(buf2.hasRemaining()){
System.out.print((char) buf2.get());
}
buf.clear();
buf2.clear();
bytesRead = inChannel.read(bufferArray);
}
aFile.close();
// Gather
FileOutputStream fileInputStream = new FileOutputStream("file1.txt");
FileChannel outChannel = fileInputStream.getChannel();
outChannel.write(bufferArray);
fileInputStream.close();
}
}
Pipe
Exam4.java:
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;
public class Exam4 {
public static void main(String[] args) throws IOException {
Pipe pipe = Pipe.open();
// 寫入Pipe
Pipe.SinkChannel sinkChannel = pipe.sink();
// 寫入數據
String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
while (buf.hasRemaining()) {
sinkChannel.write(buf);
}
// 從管道讀取數據
Pipe.SourceChannel sourceChannel = pipe.source();
ByteBuffer buff = ByteBuffer.allocate(10);
int bytesRead = sourceChannel.read(buff);
while (bytesRead != -1) {
buff.flip();
while (buff.hasRemaining()) {
System.out.print((char) buff.get());
}
buff.clear();
bytesRead = sourceChannel.read(buff);
}
sourceChannel.close();
sinkChannel.close();
}
}