Random Access Files
来源:互联网 发布:windows使用VNC 编辑:程序博客网 时间:2024/06/06 02:30
Random access files permit nonsequential, or random, access to a file’s contents. To access a file randomly, you open the file, seek a particular location, and read from or write to that file.
This functionality is possible with the SeekableByteChannel
interface. The SeekableByteChannel
interface extends channel I/O with the notion of a current position. Methods enable you to set or query the position, and you can then read the data from, or write the data to, that location. The API consists of a few, easy to use, methods:
position – Returns the channel's current position position(long) – Sets the channel's position read(ByteBuffer) – Reads bytes into the buffer from the channel write(ByteBuffer) – Writes bytes from the buffer to the channel truncate(long) – Truncates the file (or other entity) connected to the channel
the Files.newByteChannel
methods return an instance of a SeekableByteChannel
. On the default file system, you can use that channel as is, or you can cast it to a FileChannel
giving you access to more advanced features, such as mapping a region of the file directly into memory for faster access, locking a region of the file, or reading and writing bytes from an absolute location without affecting the channel’s current position.
The following code snippet opens a file for both reading and writing by using one of the newByteChannel
methods. The SeekableByteChannel
that is returned is cast to a FileChannel
. Then, 12 bytes are read from the beginning of the file, and the string “I was here!” is written at that location. The current position in the file is moved to the end, and the 12 bytes from the beginning are appended. Finally, the string, “I was here!” is appended, and the channel on the file is closed.
String s = "I was here!\n";byte data[] = s.getBytes();ByteBuffer out = ByteBuffer.wrap(data);ByteBuffer copy = ByteBuffer.allocate(12);try (FileChannel fc = (FileChannel.open(file, READ, WRITE))) { // Read the first 12 // bytes of the file. int nread; do { nread = fc.read(copy); } while (nread != -1 && copy.hasRemaining()); // Write "I was here!" at the beginning of the file. fc.position(0); while (out.hasRemaining()) fc.write(out); out.rewind(); // Move to the end of the file. Copy the first 12 bytes to // the end of the file. Then write "I was here!" again. long length = fc.size(); fc.position(length-1); copy.flip(); while (copy.hasRemaining()) fc.write(copy); while (out.hasRemaining()) fc.write(out);} catch (IOException x) { System.out.println("I/O Exception: " + x);}
- Random Access Files
- UTL_FILE - read and write file or Random Access of Files
- Random Access Iterator
- system memory之Random Access Memory (RAM)
- RAM (random access memory) 随即存贮器
- java7 NIO2(7) random access file API
- 为什么叫随机存取存储器(Random Access Memory)?
- Java API To Access Microsoft Format Files
- How to access files in a jar?
- Write and read opencv3.0 ml files(random forest)
- 【文章笔记】Random Access Compressed Sensing in Underwater Sensor Network
- 铁电存储器 - Ferroelectric Random Access Memory(FRAM)
- 对STL 中算法rotate Random access iterator 源码理解
- Reading and Writing using a Large Random Access File
- Cannot access NLS data files问题的解决办法
- Access hidden user library files | Mac OS 10.7 Lion
- myeclipse连接数据库Cannot access NLS data files or invalid
- Cannot access NLS data files or invalid environment specified
- 使用nodejs+express(4.x+)实现文件上传
- 杭电 acm 2004(成绩转换)
- 浅谈算法和数据结构: 八 平衡查找树之2-3树
- 浅谈算法和数据结构: 七 二叉查找树
- HashMap和Hashtable的区别
- Random Access Files
- 剑指offer—矩阵中的路径
- 杭电 acm 2005(第几天?)
- java学习---基础语法
- 浅谈算法和数据结构: 六 符号表及其基本实现
- 笔记
- 上传文件被nginx全部缓存的问题
- 浅谈算法和数据结构: 五 优先级队列与堆排序
- 杭电 acm 2006(求奇数的乘积)