NIO
NIO and IO
- IO works with byte streams and character streams
- NIO works with channels and buffers, data is always read from a channel into a buffer, or written from a buffer to a channel
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
- Channel
- FileChannel, reads data from and to files
- DatagramChannel, read and write data over the network via UDP
- SocketChannel, read and write data over the network via TCP
- ServerSocketChannel, listen for incoming TCP connections, like a web server does
- Buffer
- ByteBuffer
- CharBuffer
- DoubleBuffer
- FloatBuffer
- IntBuffer
- LongBuffer
- ShortBuffer
- Capacity, buffer size
- Position, current position
- Limit, the limit of how much data that is able to write into the buffer
- Allocate buffer, call allocate()
- Write to buffer
- Write data from channel to buffer by channel.read()
- Write data to buffer directly by put()
- fip, switch buffer from writing mode to reading mode
- Read from buffer
- Read data from buffer to channel by channel.write()
- Read data from buffer directly by get()
- rewind, set position back to 0, is able to recall all the data in buffer
- clear, clear up buffer and switch to writing mode
- compact, copy unread data to the beginning of buffer and switch to writing mode
- mark and reset, mark a position in buffer and reset back to the marked position later on
- Selector, allows a single thread to handle multiple channels
- 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
- Scatter, read data into more than one buffers
- Gather, output data into more than one channels
- 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
- Scatter, read data into more than one buffers
- Gather, output data into more than one channels
- 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
- examine one or more NIO Channel's, and determine which channels are ready for e.g. reading or writing
Reference