Java I/O 学习笔记(7) new I/O

<pre class="java" name="code">package files;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;

public class TestNewIO {

	public static void main(String[] args) throws IOException {
		FileChannel fc = new FileOutputStream("./output.txt").getChannel();
		String fileEncode = System.getProperty("file.encoding");
		fileEncode = "UTF-16BE";
		fc.write(ByteBuffer.wrap("正则表达式".getBytes(fileEncode)));
		fc.close();

		fc = new FileInputStream("./output.txt").getChannel();
		ByteBuffer bb = ByteBuffer.allocate(1024);
		fc.read(bb);
		bb.flip();
		Charset.forName(fileEncode).decode(bb);
		bb.rewind();
		System.out.println(fileEncode + " " + bb.asCharBuffer());
		fc.close();

		MappedByteBuffer mbf = new RandomAccessFile(
				"E:/myeclipse-8.6.0-win32.exe", "rw").getChannel().map(
				FileChannel.MapMode.READ_WRITE, 0, 500 * 1024 * 1024);
		for (int i = 0; i < 4; i++) {
			System.out.println(mbf.getChar(i * 100 * 1024 * 1024));
		}
	}
}

你可能感兴趣的:(Java I/O 学习笔记(7) new I/O)