NIO
NIO and IO
Paths
  1. import java.nio.file.*;
  2.  
  3. public class P
  4. {
  5. public static void main(String args[])
  6. {
  7. Path path = Paths.get("./");
  8.  
  9. //toAbsolutePath
  10. Path p = path.toAbsolutePath();
  11. //normalize
  12. System.out.println(p.normalize());
  13. //getFileName
  14. System.out.println(p.getFileName());
  15. //getFileSystem
  16. System.out.println(p.getFileSystem());
  17.  
  18. Path f = Paths.get("./P.java");
  19.  
  20. System.out.println(f.toAbsolutePath().normalize());
  21. //getFileName
  22. System.out.println(f.getFileName());
  23. //getFileSystem
  24. System.out.println(f.getFileSystem());
  25.  
  26. //getNameCount
  27. System.out.println(f.getNameCount());
  28. //getName
  29. for(int i = 0; i < f.getNameCount(); i++)
  30. System.out.println(f.getName(i));
  31.  
  32. //getParent
  33. System.out.println(f.getParent());
  34. //getRoot
  35. System.out.println(f.getRoot());
  36. }
  37. }
Files
  1. import java.io.*;
  2. import java.nio.file.*;
  3. import java.nio.file.attribute.*;
  4.  
  5. public class P
  6. {
  7. public static void main(String args[]) throws Exception
  8. {
  9. Path p = Paths.get("./temp.java");
  10.  
  11. //exists
  12. if(Files.exists(p, new LinkOption[]{ LinkOption.NOFOLLOW_LINKS}))
  13. {
  14. System.out.println(p.toString()+" exist ...");
  15. }
  16.  
  17. //createDirectory
  18. try
  19. {
  20. Files.createDirectory(Paths.get("./dir"));
  21. }
  22. catch (FileAlreadyExistsException e)
  23. {
  24. System.out.println(e);
  25. }
  26.  
  27. //copy
  28. try
  29. {
  30. Files.copy(p, Paths.get(Paths.get("./dir").toString(), "temp2.java"), StandardCopyOption.REPLACE_EXISTING);
  31. }
  32. catch (FileAlreadyExistsException e)
  33. {
  34. System.out.println(e);
  35. }
  36.  
  37. //move
  38. try
  39. {
  40. Files.move(Paths.get("dir/temp2.java"), Paths.get("backup.java"), StandardCopyOption.REPLACE_EXISTING);
  41. }
  42. catch (Exception e)
  43. {
  44. System.out.println(e);
  45. }
  46.  
  47. //delete
  48. try
  49. {
  50. Files.delete(Paths.get("dir"));
  51. }
  52. catch (Exception e)
  53. {
  54. System.out.println(e);
  55. }
  56.  
  57. //walkFileTree
  58. Files.walkFileTree(Paths.get("temp"), new FileVisitor() {
  59. @Override
  60. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
  61. System.out.println("pre visit dir:" + dir);
  62. return FileVisitResult.CONTINUE;
  63. }
  64. @Override
  65. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  66. System.out.println("visit file: " + file);
  67. return FileVisitResult.CONTINUE;
  68. }
  69. @Override
  70. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
  71. System.out.println("visit file failed: " + file);
  72. return FileVisitResult.CONTINUE;
  73. }
  74. @Override
  75. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
  76. System.out.println("post visit directory: " + dir);
  77. return FileVisitResult.CONTINUE;
  78. }
  79. });
  80. }
  81. }
NIO
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.file.*;
  4. import java.nio.channels.*;
  5.  
  6. public class C
  7. {
  8. public static void main(String args[]) throws IOException
  9. {
  10. RandomAccessFile aFile = new RandomAccessFile("temp.txt", "rw");
  11. FileChannel inChannel = aFile.getChannel();
  12.  
  13. ByteBuffer buf = ByteBuffer.allocate(10);//allocate 10 bytes buffer
  14.  
  15. int bytesRead;
  16. while ((bytesRead = inChannel.read(buf)) != -1) {
  17.  
  18. System.out.println("Read " + bytesRead);
  19. buf.flip();//switch buffer from put to get
  20.  
  21. while(buf.hasRemaining()){
  22. System.out.print((char) buf.get());
  23. }
  24.  
  25. buf.clear();//switch buffer from get to put
  26. System.out.println();
  27. }
  28. aFile.close();
  29. }
  30. }
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.file.*;
  4. import java.nio.channels.*;
  5.  
  6. public class C
  7. {
  8. public static void bufferInfo(Buffer buf)
  9. {
  10. System.out.println("Position: "+buf.position()+" Limit: "+buf.limit()+" Capacity: "+buf.capacity());
  11. }
  12.  
  13. public static void main(String args[]) throws IOException
  14. {
  15. RandomAccessFile aFile = new RandomAccessFile("temp.txt", "rw");
  16. FileChannel inChannel = aFile.getChannel();
  17.  
  18. ByteBuffer buf = ByteBuffer.allocate(5);//allocate 10 bytes buffer
  19.  
  20. bufferInfo(buf);
  21.  
  22. int bytesRead;
  23.  
  24. bytesRead = inChannel.read(buf);//read data from channel to buffer
  25.  
  26. System.out.println("Read "+bytesRead+" bytes ...");
  27.  
  28. bufferInfo(buf);
  29.  
  30. buf.flip();//switch from writing mode to reading mode
  31.  
  32. System.out.println("Read: "+(char)buf.get());//read one byte from buffer
  33.  
  34. bufferInfo(buf);
  35.  
  36. buf.rewind();//set position back to 0
  37.  
  38. System.out.println("Read: "+(char)buf.get());//read one byte from buffer
  39.  
  40. aFile.close();
  41. }
  42. }
Scatter and Gather
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4.  
  5. public class S
  6. {
  7. public static void buffInfo(Buffer buf)
  8. {
  9. System.out.println("Position: "+buf.position()+" Limit: "+buf.limit()+" Capacity: "+buf.capacity());
  10. }
  11.  
  12. public static void main(String args[]) throws IOException
  13. {
  14. ByteBuffer header = ByteBuffer.allocate(5);
  15. ByteBuffer body = ByteBuffer.allocate(5);
  16.  
  17. ByteBuffer[] bufferArray = { header, body };
  18.  
  19. //read
  20. FileChannel channel = (new RandomAccessFile("temp.txt", "rw")).getChannel();
  21.  
  22. channel.read(bufferArray);
  23.  
  24. buffInfo(header);
  25. buffInfo(body);
  26.  
  27. header.flip();
  28. body.flip();
  29.  
  30. //write
  31. FileChannel output = (new RandomAccessFile("temp2.txt", "rw").getChannel());
  32.  
  33. output.write(bufferArray);
  34. }
  35. }
Channel to Channel Transfer
  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4.  
  5. public class T
  6. {
  7. public static void main(String args[]) throws IOException
  8. {
  9. FileChannel from = (new RandomAccessFile("temp.txt", "rw")).getChannel();
  10. FileChannel to = (new RandomAccessFile("temp2.txt", "rw")).getChannel();
  11.  
  12. to.transferFrom(from, 0, from.size());
  13.  
  14. from.close();
  15. to.close();
  16. }
  17. }
Selector
Reference