Creating a Very Large File Using Mapping

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Creating a Very Large File Using Mapping

This Java example shows how to create a very large file using mapping.

[edit] Java Source Code

  • Package: com.bruceeckel
  • File: LargeMappedFiles.java
package com.bruceeckel;
 
//: c12:LargeMappedFiles.java
//Creating a very large file using mapping.
//{RunByHand}
//{Clean: test.dat}
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
 
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
public class LargeMappedFiles {
    static int length = 0x8FFFFFF; // 128 Mb
 
    public static void main(String[] args) throws Exception {
	MappedByteBuffer out = new RandomAccessFile("test.dat", "rw")
		.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
	for (int i = 0; i < length; i++)
	    out.put((byte) 'x');
	System.out.println("Finished writing");
	for (int i = length / 2; i < length / 2 + 6; i++)
	    System.out.print((char) out.get(i));
    }
}

[edit] What Result You Can Get

Run the program, you will get:


Finished writing
xxxxxx

[edit] Required External Library for this Java Example

Need nothing.

http://www.ase2009.com/ online casino 132 http://www.hellzyea.com/health health insurance 8DDD http://www.yourautoinsurancesite.com/ auto insurance quotes 135811 http://www.makemeasammich.com/ auto insurance mcnxu
Personal tools