java NIO FileChannel

http://www.cnblogs.com/puyangsky/p/5840873.html

2.2.2 File channel

2.2.2.1 Open

FileChannel can only be instantiated through a factory method, that is to call the getChannel() method of RandomAccessFile, FileInputStream and FileOutputStream. Such as:

RandomAccessFile file = new RandomAccessFile("a.txt", "r");
FileChannel fc = file.getChannel();
2.2.2.2 Use

First look at the method handle provided by FileChannel:

public abstract int read(ByteBuffer dst) throws IOException;//把通道中数据传到目的缓冲区中,dst是destination的缩写 public abstract int write(ByteBuffer src) throws IOException;//把源缓冲区中的内容写到指定的通道中去

It can be seen from the handle that FileChannel can be read and written, and it is full-duplex. The following example is used to show how FileChannel reads and writes.

public class FileChannelTest {

    public static void readFile(String path) throws IOException { FileChannel fc = new FileInputStream(path).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(128); StringBuilder sb = new StringBuilder(); while ((fc.read(buffer)) >= 0) { //翻转指针 buffer.flip(); //remaining = limit - position byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); String string = new String(bytes, "UTF-8"); sb.append(string); //清空buffer buffer.clear(); } System.out.println(sb.toString()); } public static void writeFile(String path, String string) throws IOException { FileChannel fc = new FileOutputStream(path).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(10); int current = 0; int len = string.getBytes().length; while (current < len) { for (int i=0;i<10;i++) { if (current+i>=len) break; buffer.put(string.getBytes()[current+i]); } current += buffer.position(); buffer.flip(); fc.write(buffer); buffer.clear(); } } public static void main(String[] args) throws IOException { String in = "D:/in.txt"; String out = "D:/out.txt"; readFile(in); writeFile(out, "hello world"); readFile(out); } }

Analyze the above code. In the readFile() function, the FileChannel object is obtained through FileInputStream.getChannel(), and the ByteBuffer object is created. Then, the read method of FileChannel is used to fill the buffer. After getting the filled buffer, we will use the buffer's current pointer. Flip it over and then use the get method of the buffer to put the data into the byte array, and then you can read the data.

The whole process of reading files is a little more troublesome than the native I/O method, but if we think of data as a pile of coal mines, ByteBuffer as a minecart for coal, and FileChannel as a coal mine, Then the above process has evolved into: first open a mine road, and then load the coal mine in a trolley and transport it out. The memory of the image is more conducive to understanding this process.

The writeFile() function is similar. In order to better understand the properties of the Buffer, I deliberately set the size of the buffer to 10, and the length of the string to be written is 11 bytes. First, get the FileChannel object through the FileOutputStream.getChannel() method, and create a 10-byte buffer, then define an integer variable current to point to the current subscript of the string to be written, and put 10 items into the buffer each time bytes, and update the current, the buffer.position() method can get the position of the pointer after the buffer is filled, that is, the number of bytes in the buffer, then flip the pointer, and finally pass the FileChannel.write(buffer) method to the buffer. Write to file.

Also consider the visualization process: we first load the coal mine into the trolley (buffer.put()), open a mine tunnel to the mine (FileOutputStream.getChannel()), and then transport the coal mine in (FileChannel.write( buffer)). It's still easy to understand!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324484461&siteId=291194637