NIO
NIO and IO
Paths
import java.nio.file.*;

public class P
{
	public static void main(String args[])
	{
		Path path = Paths.get("./");

		//toAbsolutePath
		Path p = path.toAbsolutePath();
		//normalize
		System.out.println(p.normalize());
		//getFileName
		System.out.println(p.getFileName());
		//getFileSystem
		System.out.println(p.getFileSystem());

		Path f = Paths.get("./P.java");

		System.out.println(f.toAbsolutePath().normalize());
		//getFileName
		System.out.println(f.getFileName());
		//getFileSystem
		System.out.println(f.getFileSystem());

		//getNameCount
		System.out.println(f.getNameCount());
		//getName
		for(int i = 0; i < f.getNameCount(); i++)
			System.out.println(f.getName(i));

		//getParent
		System.out.println(f.getParent());
		//getRoot
		System.out.println(f.getRoot());
	}
}
			
Files
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;

public class P
{
	public static void main(String args[]) throws Exception
	{
		Path p = Paths.get("./temp.java");

		//exists
		if(Files.exists(p, new LinkOption[]{ LinkOption.NOFOLLOW_LINKS}))
		{
			System.out.println(p.toString()+" exist ...");
		}

		//createDirectory
		try
		{
			Files.createDirectory(Paths.get("./dir"));
		}
		catch (FileAlreadyExistsException e)
		{
			System.out.println(e);
		}

		//copy
		try
		{
			Files.copy(p, Paths.get(Paths.get("./dir").toString(), "temp2.java"), StandardCopyOption.REPLACE_EXISTING);
		}
		catch (FileAlreadyExistsException e)
		{
			System.out.println(e);
		}

		//move
		try
		{
			Files.move(Paths.get("dir/temp2.java"), Paths.get("backup.java"), StandardCopyOption.REPLACE_EXISTING);
		}
		catch (Exception e)
		{
			System.out.println(e);
		}

		//delete
		try
		{
			Files.delete(Paths.get("dir"));
		}
		catch (Exception e)
		{
			System.out.println(e);
		}

		//walkFileTree
		Files.walkFileTree(Paths.get("temp"), new FileVisitor() {
 		 @Override
  		public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
    			System.out.println("pre visit dir:" + dir);
    			return FileVisitResult.CONTINUE;
  		}

  		@Override
  		public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
    			System.out.println("visit file: " + file);
    			return FileVisitResult.CONTINUE;
  		}

  		@Override
  		public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
    			System.out.println("visit file failed: " + file);
    			return FileVisitResult.CONTINUE;
  		}

  		@Override
  		public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
    			System.out.println("post visit directory: " + dir);
    			return FileVisitResult.CONTINUE;
  		}
});
	}
}
			
NIO
import java.io.*;
import java.nio.*;
import java.nio.file.*;
import java.nio.channels.*;

public class C
{
	public static void main(String args[]) throws IOException
	{
		RandomAccessFile aFile = new RandomAccessFile("temp.txt", "rw");
	    	FileChannel inChannel = aFile.getChannel();

	    	ByteBuffer buf = ByteBuffer.allocate(10);//allocate 10 bytes buffer

	    	int bytesRead;
	    	while ((bytesRead = inChannel.read(buf)) != -1) {

		  	System.out.println("Read " + bytesRead);
		  	buf.flip();//switch buffer from put to get

		  	while(buf.hasRemaining()){
		      		System.out.print((char) buf.get());
		  	}

		  	buf.clear();//switch buffer from get to put
			System.out.println();
	    	}
	    	aFile.close();
	}
}
			
import java.io.*;
import java.nio.*;
import java.nio.file.*;
import java.nio.channels.*;

public class C
{
	public static void bufferInfo(Buffer buf)
	{
		System.out.println("Position: "+buf.position()+" Limit: "+buf.limit()+" Capacity: "+buf.capacity());
	}

	public static void main(String args[]) throws IOException
	{
		RandomAccessFile aFile = new RandomAccessFile("temp.txt", "rw");
	    	FileChannel inChannel = aFile.getChannel();

	    	ByteBuffer buf = ByteBuffer.allocate(5);//allocate 10 bytes buffer

		bufferInfo(buf);

		int bytesRead;

		bytesRead = inChannel.read(buf);//read data from channel to buffer

		System.out.println("Read "+bytesRead+" bytes ...");

		bufferInfo(buf);

		buf.flip();//switch from writing mode to reading mode

		System.out.println("Read: "+(char)buf.get());//read one byte from buffer

		bufferInfo(buf);

		buf.rewind();//set position back to 0

		System.out.println("Read: "+(char)buf.get());//read one byte from buffer

	    	aFile.close();
	}
}
			
Scatter and Gather
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class S
{
	public static void buffInfo(Buffer buf)
	{
		System.out.println("Position: "+buf.position()+" Limit: "+buf.limit()+" Capacity: "+buf.capacity());
	}

	public static void main(String args[]) throws IOException
	{
		ByteBuffer header = ByteBuffer.allocate(5);
		ByteBuffer body   = ByteBuffer.allocate(5);

		ByteBuffer[] bufferArray = { header, body };

		//read
		FileChannel channel = (new RandomAccessFile("temp.txt", "rw")).getChannel();

		channel.read(bufferArray);

		buffInfo(header);
		buffInfo(body);

		header.flip();
		body.flip();

		//write
		FileChannel output = (new RandomAccessFile("temp2.txt", "rw").getChannel());

		output.write(bufferArray);
	}
}
			
Channel to Channel Transfer
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class T
{
	public static void main(String args[]) throws IOException
	{
		FileChannel from = (new RandomAccessFile("temp.txt", "rw")).getChannel();
		FileChannel to = (new RandomAccessFile("temp2.txt", "rw")).getChannel();

		to.transferFrom(from, 0, from.size());

		from.close();
		to.close();
	}
}
			
Selector
Reference