免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1975 | 回复: 1
打印 上一主题 下一主题

NIO学习总结 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 17:35 |只看该作者 |倒序浏览
NIO学习总结







Java代码
  1. 1./**   
  2. 2.  * 使用传统的I/O读取文件内容  
  3. 3.  * @param filePath  文件路径  
  4. 4.   * @throws IOException  
  5. 5.*/  
  6. 6. public static void ioRead(String filePath) throws IOException {   
  7. 7.        File file = new File(filePath);   
  8. 8.        FileInputStream fis = new FileInputStream(file);   
  9. 9.        byte[] b = new byte[1024];   
  10. 10.        fis.read(b);   
  11. 11.        System.out.println(new String(b));   
  12. 12. }   
  13. 13.  
  14. 14./**  
  15. 15.  * 使用NIO读取文件内容  
  16. 16.  * @param filePath 文件路径  
  17. 17.  * @throws IOException  
  18. 18.*/  
  19. 19.  public static void nioRead(String filePath) throws IOException {   
  20. 20.        File file = new File(filePath);   
  21. 21.        FileInputStream fis = new FileInputStream(file);   
  22. 22.        FileChannel channel = fis.getChannel();   
  23. 23.        ByteBuffer b = ByteBuffer.allocate(1024);   
  24. 24.        channel.read(b);   
  25. 25.        byte[] by = b.array();   
  26. 26.        System.out.println(new String(by));   
  27. 27.  }   
  28. 28.      
  29. 29./**  
  30. 30.  * 传统I/O写文件  
  31. 31.  *   
  32. 32.  * @param filePath  文件路径  
  33. 33.  * @throws IOException  
  34. 34.*/  
  35. 35.  public static void ioWrite(String filePath) throws IOException   
  36. 36.  {   
  37. 37.        File file = new File(filePath);   
  38. 38.        FileOutputStream fos = new FileOutputStream(file);   
  39. 39.        String[] contents = new String[] { "qian", "hao" };   
  40. 40.        for (String content : contents)   
  41. 41.        {   
  42. 42.       byte[] b = content.getBytes(Charset.forName("UTF-8"));   
  43. 43.       fos.write(b);   
  44. 44.        }   
  45. 45.   }   
  46. 46.  
  47. 47./**  
  48. 48.  * NIO写文件  
  49. 49.  * @param filePath 文件路径  
  50. 50.  * @throws IOException  
  51. 51.*/  
  52. 52. public static void nioWrite(String filePath) throws IOException   
  53. 53. {   
  54. 54.        File file = new File(filePath);   
  55. 55.        FileOutputStream fos = new FileOutputStream(file);   
  56. 56.        // 获取文件通道   
  57. 57.         FileChannel channel = fos.getChannel();   
  58. 58.        // 设置文件缓冲区   
  59. 59.         ByteBuffer buffer = ByteBuffer.allocate(1024);   
  60. 60.        String[] contents = new String[] { "qian", "hao" };   
  61. 61.        // 将数据读入缓冲区中   
  62. 62.         for (String content : contents)   
  63. 63.       {   
  64. 64.      buffer.put(content.getBytes());   
  65. 65.       }   
  66. 66.       // 通道反转(将读通道转化为写通道)   
  67. 67.      buffer.flip();   
  68. 68.       // 将文件写入写通道中   
  69. 69.      channel.write(buffer);   
  70. 70. }   
  71. 71.  
  72. 72./**  
  73. 73.  * 将一个文件内容复制到另外一个文件中  
  74. 74.  * @param resource  源文件路径  
  75. 75.  * @param destination 目标文件路径  
  76. 76.  * @throws IOException  
  77. 77.*/  
  78. 78. public static void nioCopyFile(String resource, String destination)   
  79. 79.                throws IOException   
  80. 80.{   
  81. 81.       // 设置文件输入流   
  82. 82.        FileInputStream fis = new FileInputStream(resource);   
  83. 83.       // 设置文件输出流   
  84. 84.        FileOutputStream fos = new FileOutputStream(destination);   
  85. 85.       // 设置输入通道   
  86. 86.        FileChannel readChannel = fis.getChannel();   
  87. 87.       // 设置输出通道   
  88. 88.        FileChannel writeChannel = fos.getChannel();   
  89. 89.       // 创建缓冲区   
  90. 90.        ByteBuffer buffer = ByteBuffer.allocate(1024);   
  91. 91.       // 复制文件   
  92. 92.        while (true)   
  93. 93.       {   
  94. 94.        // 清空缓冲,使其接受新的数据   
  95. 95.    buffer.clear();   
  96. 96.        // 从输入通道中将数据写入缓冲区中   
  97. 97.    int len = readChannel.read(buffer);   
  98. 98.        // 判断是否文件已读取完成   
  99. 99.    if (len == -1)   
  100. 100.         {   
  101. 101.       break;   
  102. 102.    }   
  103. 103.        // 将缓冲区的数据些到另外一个通道,(输入通道反转到输出通道)   
  104. 104.       buffer.flip();   
  105. 105.        // 从输出通道将数据写到缓冲区,写入文件中   
  106. 106.       writeChannel.write(buffer);   
  107. 107.    }   
  108. 108. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-19 17:35 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP