FileChannel,是內存與磁盤文件的通道。
========================================================
優勢:
- 多線程並發讀寫,並發性;
- IO讀寫性能提高(OS負責),也可引做共享內存,減少IO操作,提升並發性;
- 應用crash,保證這部分內容還能寫的進去文件。在我們調用channel.write(bytebuffer)之后,具體何時寫入磁盤、bytebuffer中內容暫存於哪里(os cache)等相關一系列問題,就交由OS本身負責了。
========================================================
- read(),write()
- lock(), tryLock()
- position(), size(), truncate()
- transferFrom(), transferTo()
- map()
【方法的具體含義】:http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/nio/channels/FileChannel.html
=========================================================
備注:
- JAVA操作大數據量的文件利用FileReader的會把所有的內容加載到內存中,因此沒有意義。
如果要使用BIO,建議使用java.io.RandomAccessFile來做,讀取部分信息。
如果要使用NIO,建議使用java.nio.channels.FileChannel,使用虛擬內存來Mapping大文件。
http://simpleframework.net/bbs/835/7894.html
關於File,簡單的:
File f = new File("D:/hutuTesting.txt");
System.out.println(f.getName()); // hutuTesting.txt
System.out.println(f.getAbsolutePath()); // D:\hutuTesting.txt
System.out.println(f.toString()); // D:\hutuTesting.txt
