[java][nio]MappedByteBuffer 큰 파일 수정

1050 단어 자바


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/**
 * 
 * MappedByteBuffer          .
 *
 */
public class LargeMappedFiles {

	static int length = 128 * 1024 * 1024;//128m
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		RandomAccessFile file = new RandomAccessFile("d:\\a.txt","rw");
		//MappedByteBuffer         .                .
		MappedByteBuffer out = file.getChannel().map(FileChannel.MapMode.READ_WRITE, file.length(), length);
		for(int i=0; i<length; i++){
			out.put((byte)'x');
		}
		System.out.println("Finish writing.");
		for(int i = length/2; i<(length/2 + 6); i++){
			System.out.println((char)out.get(i));
		}
		file.close();
	}

}


좋은 웹페이지 즐겨찾기