一、CopyFile
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class CopyFile
{
static public void main( String args[] ) throws Exception {
if (args.length<2) {
System.err.println( "Usage: java CopyFile infile outfile" );
System.exit( 1 );
}
String infile = args[0];
String outfile = args[1];
FileInputStream fin = new FileInputStream( infile );
FileOutputStream fout = new FileOutputStream( outfile );
FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocate( 1024 );
while (true) {
buffer.clear();
int r = fcin.read( buffer );
if (r==-1) {
break;
}
buffer.flip();
fcout.write( buffer );
}
}
}
二、FastCopyFile
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class FastCopyFile
{
static public void main( String args[] ) throws Exception {
if (args.length<2) {
System.err.println( "Usage: java FastCopyFile infile outfile" );
System.exit( 1 );
}
String infile = args[0];
String outfile = args[1];
FileInputStream fin = new FileInputStream( infile );
FileOutputStream fout = new FileOutputStream( outfile );
FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 );
while (true) {
buffer.clear();
int r = fcin.read( buffer );
if (r==-1) {
break;
}
buffer.flip();
fcout.write( buffer );
}
}
}
三、ReadAndShow
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class ReadAndShow
{
static public void main( String args[] ) throws Exception {
FileInputStream fin = new FileInputStream( "readandshow.txt" );
FileChannel fc = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate( 1024 );
fc.read( buffer );
buffer.flip();
int i=0;
while (buffer.remaining()>0) {
byte b = buffer.get();
System.out.println( "Character "+i+": "+((char)b) );
i++;
}
fin.close();
}
}
四、UseCharsets
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
public class UseCharsets
{
static public void main( String args[] ) throws Exception {
String inputFile = "samplein.txt";
String outputFile = "sampleout.txt";
RandomAccessFile inf = new RandomAccessFile( inputFile, "r" );
RandomAccessFile outf = new RandomAccessFile( outputFile, "rw" );
long inputLength = new File( inputFile ).length();
FileChannel inc = inf.getChannel();
FileChannel outc = outf.getChannel();
MappedByteBuffer inputData =
inc.map( FileChannel.MapMode.READ_ONLY, 0, inputLength );
Charset latin1 = Charset.forName( "ISO-8859-1" );
CharsetDecoder decoder = latin1.newDecoder();
CharsetEncoder encoder = latin1.newEncoder();
CharBuffer cb = decoder.decode( inputData );
// Process char data here
ByteBuffer outputData = encoder.encode( cb );
outc.write( outputData );
inf.close();
outf.close();
}
}
五、UseFileLocks
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class UseFileLocks
{
static private final int start = 10;
static private final int end = 20;
static public void main( String args[] ) throws Exception {
// Get file channel
RandomAccessFile raf = new RandomAccessFile( "usefilelocks.txt", "rw" );
FileChannel fc = raf.getChannel();
// Get lock
System.out.println( "trying to get lock" );
FileLock lock = fc.lock( start, end, false );
System.out.println( "got lock!" );
// Pause
System.out.println( "pausing" );
try { Thread.sleep( 3000 ); } catch( InterruptedException ie ) {}
// Release lock
System.out.println( "going to release lock" );
lock.release();
System.out.println( "released lock" );
raf.close();
}
}
六、WriteSomeBytes
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class WriteSomeBytes
{
static private final byte message[] = { 83, 111, 109, 101, 32,
98, 121, 116, 101, 115, 46 };
static public void main( String args[] ) throws Exception {
FileOutputStream fout = new FileOutputStream( "writesomebytes.txt" );
FileChannel fc = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocate( 1024 );
for (int i=0; i<message.length; ++i) {
buffer.put( message[i] );
}
buffer.flip();
fc.write( buffer );
fout.close();
}
}
七、UseMappedFile
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class UseMappedFile
{
static private final int start = 0;
static private final int size = 1024;
static public void main( String args[] ) throws Exception {
RandomAccessFile raf = new RandomAccessFile( "usemappedfile.txt", "rw" );
FileChannel fc = raf.getChannel();
MappedByteBuffer mbb = fc.map( FileChannel.MapMode.READ_WRITE,
start, size );
mbb.put( 0, (byte)97 );
mbb.put( 1023, (byte)122 );
raf.close();
}
}