亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
NIO學(xué)習(xí)總結(jié)
[打印本頁(yè)]
作者:
聽(tīng)老歌
時(shí)間:
2012-03-19 17:35
標(biāo)題:
NIO學(xué)習(xí)總結(jié)
NIO學(xué)習(xí)總結(jié)
Java代碼
1./**
2. * 使用傳統(tǒng)的I/O讀取文件內(nèi)容
3. * @param filePath 文件路徑
4. * @throws IOException
5.*/
6. public static void ioRead(String filePath) throws IOException {
7. File file = new File(filePath);
8. FileInputStream fis = new FileInputStream(file);
9. byte[] b = new byte[1024];
10. fis.read(b);
11. System.out.println(new String(b));
12. }
13.
14./**
15. * 使用NIO讀取文件內(nèi)容
16. * @param filePath 文件路徑
17. * @throws IOException
18.*/
19. public static void nioRead(String filePath) throws IOException {
20. File file = new File(filePath);
21. FileInputStream fis = new FileInputStream(file);
22. FileChannel channel = fis.getChannel();
23. ByteBuffer b = ByteBuffer.allocate(1024);
24. channel.read(b);
25. byte[] by = b.array();
26. System.out.println(new String(by));
27. }
28.
29./**
30. * 傳統(tǒng)I/O寫(xiě)文件
31. *
32. * @param filePath 文件路徑
33. * @throws IOException
34.*/
35. public static void ioWrite(String filePath) throws IOException
36. {
37. File file = new File(filePath);
38. FileOutputStream fos = new FileOutputStream(file);
39. String[] contents = new String[] { "qian", "hao" };
40. for (String content : contents)
41. {
42. byte[] b = content.getBytes(Charset.forName("UTF-8"));
43. fos.write(b);
44. }
45. }
46.
47./**
48. * NIO寫(xiě)文件
49. * @param filePath 文件路徑
50. * @throws IOException
51.*/
52. public static void nioWrite(String filePath) throws IOException
53. {
54. File file = new File(filePath);
55. FileOutputStream fos = new FileOutputStream(file);
56. // 獲取文件通道
57. FileChannel channel = fos.getChannel();
58. // 設(shè)置文件緩沖區(qū)
59. ByteBuffer buffer = ByteBuffer.allocate(1024);
60. String[] contents = new String[] { "qian", "hao" };
61. // 將數(shù)據(jù)讀入緩沖區(qū)中
62. for (String content : contents)
63. {
64. buffer.put(content.getBytes());
65. }
66. // 通道反轉(zhuǎn)(將讀通道轉(zhuǎn)化為寫(xiě)通道)
67. buffer.flip();
68. // 將文件寫(xiě)入寫(xiě)通道中
69. channel.write(buffer);
70. }
71.
72./**
73. * 將一個(gè)文件內(nèi)容復(fù)制到另外一個(gè)文件中
74. * @param resource 源文件路徑
75. * @param destination 目標(biāo)文件路徑
76. * @throws IOException
77.*/
78. public static void nioCopyFile(String resource, String destination)
79. throws IOException
80.{
81. // 設(shè)置文件輸入流
82. FileInputStream fis = new FileInputStream(resource);
83. // 設(shè)置文件輸出流
84. FileOutputStream fos = new FileOutputStream(destination);
85. // 設(shè)置輸入通道
86. FileChannel readChannel = fis.getChannel();
87. // 設(shè)置輸出通道
88. FileChannel writeChannel = fos.getChannel();
89. // 創(chuàng)建緩沖區(qū)
90. ByteBuffer buffer = ByteBuffer.allocate(1024);
91. // 復(fù)制文件
92. while (true)
93. {
94. // 清空緩沖,使其接受新的數(shù)據(jù)
95. buffer.clear();
96. // 從輸入通道中將數(shù)據(jù)寫(xiě)入緩沖區(qū)中
97. int len = readChannel.read(buffer);
98. // 判斷是否文件已讀取完成
99. if (len == -1)
100. {
101. break;
102. }
103. // 將緩沖區(qū)的數(shù)據(jù)些到另外一個(gè)通道,(輸入通道反轉(zhuǎn)到輸出通道)
104. buffer.flip();
105. // 從輸出通道將數(shù)據(jù)寫(xiě)到緩沖區(qū),寫(xiě)入文件中
106. writeChannel.write(buffer);
107. }
108. }
復(fù)制代碼
作者:
如果有一天21
時(shí)間:
2012-03-19 17:35
謝謝分享
歡迎光臨 Chinaunix (http://72891.cn/)
Powered by Discuz! X3.2