亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 2044 | 回復(fù): 1
打印 上一主題 下一主題

NIO學(xué)習(xí)總結(jié) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-03-19 17:35 |只看該作者 |倒序?yàn)g覽
NIO學(xué)習(xí)總結(jié)







Java代碼
  1. 1./**   
  2. 2.  * 使用傳統(tǒng)的I/O讀取文件內(nèi)容  
  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讀取文件內(nèi)容  
  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.  * 傳統(tǒng)I/O寫(xiě)文件  
  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寫(xiě)文件  
  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.        // 設(shè)置文件緩沖區(qū)   
  59. 59.         ByteBuffer buffer = ByteBuffer.allocate(1024);   
  60. 60.        String[] contents = new String[] { "qian", "hao" };   
  61. 61.        // 將數(shù)據(jù)讀入緩沖區(qū)中   
  62. 62.         for (String content : contents)   
  63. 63.       {   
  64. 64.      buffer.put(content.getBytes());   
  65. 65.       }   
  66. 66.       // 通道反轉(zhuǎn)(將讀通道轉(zhuǎn)化為寫(xiě)通道)   
  67. 67.      buffer.flip();   
  68. 68.       // 將文件寫(xiě)入寫(xiě)通道中   
  69. 69.      channel.write(buffer);   
  70. 70. }   
  71. 71.  
  72. 72./**  
  73. 73.  * 將一個(gè)文件內(nèi)容復(fù)制到另外一個(gè)文件中  
  74. 74.  * @param resource  源文件路徑  
  75. 75.  * @param destination 目標(biāo)文件路徑  
  76. 76.  * @throws IOException  
  77. 77.*/  
  78. 78. public static void nioCopyFile(String resource, String destination)   
  79. 79.                throws IOException   
  80. 80.{   
  81. 81.       // 設(shè)置文件輸入流   
  82. 82.        FileInputStream fis = new FileInputStream(resource);   
  83. 83.       // 設(shè)置文件輸出流   
  84. 84.        FileOutputStream fos = new FileOutputStream(destination);   
  85. 85.       // 設(shè)置輸入通道   
  86. 86.        FileChannel readChannel = fis.getChannel();   
  87. 87.       // 設(shè)置輸出通道   
  88. 88.        FileChannel writeChannel = fos.getChannel();   
  89. 89.       // 創(chuàng)建緩沖區(qū)   
  90. 90.        ByteBuffer buffer = ByteBuffer.allocate(1024);   
  91. 91.       // 復(fù)制文件   
  92. 92.        while (true)   
  93. 93.       {   
  94. 94.        // 清空緩沖,使其接受新的數(shù)據(jù)   
  95. 95.    buffer.clear();   
  96. 96.        // 從輸入通道中將數(shù)據(jù)寫(xiě)入緩沖區(qū)中   
  97. 97.    int len = readChannel.read(buffer);   
  98. 98.        // 判斷是否文件已讀取完成   
  99. 99.    if (len == -1)   
  100. 100.         {   
  101. 101.       break;   
  102. 102.    }   
  103. 103.        // 將緩沖區(qū)的數(shù)據(jù)些到另外一個(gè)通道,(輸入通道反轉(zhuǎn)到輸出通道)   
  104. 104.       buffer.flip();   
  105. 105.        // 從輸出通道將數(shù)據(jù)寫(xiě)到緩沖區(qū),寫(xiě)入文件中   
  106. 106.       writeChannel.write(buffer);   
  107. 107.    }   
  108. 108. }
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-03-19 17:35 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP