大部分内容取自thinking in java。。宝典级的书
1. package
2.
3. import
4. import
5. import
6. import
7. import
8.
9. /**
10. * NIO速度的提高,是由于使用了更接近于操作系统I/O处理方式的结构:channels和buffers.
11. * 如果将channel比喻成墙角,那么buffer就是一把锄头。
12. * 我们不会直接搬墙角。但是,只要锄头挥得好......
13. *
14. * 直接和channel打交道的只有一种最低层实现的类型:ByteBuffer
15. * 创建的时候甚至需要指定分配多少内存空间,它仅实现了byte格式及基本数据格式(primitive data)
16. * 的输入输出函数,甚至不可以直接输入一个Object或者String。
17. *
18. * channel的作用非常简单:处理ByteBuffer的读写,锁定文件块内容
19. * channel的构造方法来自于对原I/O系统中3个基于byte流的对象的修改:
20. * FileInputStream,FileOutputStream,RandomAccessFile
21. *
22. * @author wei.songw
23. * Oct 16, 2008 3:54:16 PM
24. */
25. public class
26.
27. private static final int BSIZE = 1024;
28.
29. public static void main(String[] args) throws
30.
31. //write file
32. new FileOutputStream("abc.txt").getChannel();
33. "some string".getBytes()));
34. fc.close();
35.
36. new RandomAccessFile("abc.txt","rw").getChannel();
37. //go the end
38. "more string".getBytes()));
39. fc.close();
40.
41. new FileInputStream("abc.txt").getChannel();
42. ByteBuffer bf = ByteBuffer.allocate(BSIZE);
43. fc.read(bf);
44. //Notice here.flip filter!!
45. while(bf.hasRemaining()) {
46. char)bf.get());
47. }
48. }
49. }
1. package
2.
3. import
4. import
5.
6. /**
7. * ByteBuffer虽然仅持有byte数组,但是API提供了一些方法处理primitives
8. * (给对象提供相应的配套操作,往往是一个好的OO设计理念。
9. * 比如提供Collection,就得配套提供的Iterator遍历器,而不要让用户自己
10. * 去实现迭代或者是依赖异常判断是否越界)
11. *
12. * (为API提供一致性的结构和函数命名规范也是一种好习惯。。asCharBuffer,
13. * asIntBuffer , asDoubleBuffer , getInt, getLong ....)
14. * @author wei.songw
15. * Oct 16, 2008 8:19:49 PM
16. */
17. public class
18.
19. private static final int BSIZE = 1024;
20.
21. public static void main(String[] args) throws
22. //filled by 0
23.
24. //char
25. "heihei");
26. char
27. while((c = bb.getChar()) != 0) {
28. " ");
29. }
30.
31. bb.rewind();
32. //int
33. 12345);
34. System.out.print(bb.getInt());
35.
36. //batches
37. bb.clear();
38. IntBuffer ib = bb.asIntBuffer();
39. new int[]{11,22,33,44,55});
40. 3));
41. }
42. }
43.
1. package
2.
3. import
4. import
5. import
6.
7. /**
8. * 网上copy的例子,内存映射文件主要是来操作大文件.最大可达2GB.
9. * @author wei.songw
10. * Oct 16, 2008 10:34:52 PM
11. */
12. public class
13.
14. static int length = 0x8FFFFFF; // 128 Mb
15. public static void main(String[] args) throws
16. MappedByteBuffer out =
17. new RandomAccessFile("test.dat", "rw").getChannel()
18. 0, length);
19. for(int i = 0; i < length; i++)
20. byte)'x');
21. "Finished writing");
22. for(int i = length/2; i < length/2 + 6; i++)
23. char)out.get(i)); //read file
24. }
25. }
26.
1. package
2.
3. import
4. import
5. import
6.
7. /**
8. * 主要是研究tryLock(),Lock()的API.
9. * 无参的全文锁定支持文件大小的改变,
10. * 而固定大小的部分锁定则不会自动适应文件的增长(
11. * if a locked region initially contains the end of the file
12. * and the file grows beyond the region then the new portion of the file
13. * will not be covered by the lock.)
14. *
15. *
16. * 文件锁同样支持MemoryMappedFile
17. *
18. * @author wei.songw
19. * Oct 16, 2008 11:16:14 PM
20. */
21. public class
22.
23. public static void main(String[] args) throws
24. new FileOutputStream("abc.txt");
25. //locking the hole file
26. FileLock fl = fos.getChannel().tryLock();
27. if(fl != null) {
28. "file locked");
29. //do your work
30. 2000);
31. fl.release();
32. "Released lock");
33. }
34.
35. //for locking a part of the file,use:
36. //FileLock tryLock(long position, long size, boolean shared)
37. //FileLock Lock(long position, long size, boolean shared)
38.
39.
40. fos.close();
41. }
42. }