"0" README
0.1) This article describes the move from core Java Volume 2, which is designed to understand the Java flow and file--randomaccessfile class parsing knowledge;
0.1) This article goes from: http://blog.csdn.net/akon_vm/article/details/7429245, for complete my DIY code, please visit https:/ /github.com/pacosontang/core-java-volume/blob/master/corejavaadvanced/chapter1/binaryio.java
"1" Randomaccessfile class
1.1) Randomaccessfile is used to access files that hold data records, you can use the Seek () method to access the records and read and write them. The sizes of these records do not have to be the same, but their size and position must be knowable. However, this class is limited to manipulating files.
1.2) Randomaccessfile does not belong to the InputStream and OutputStream class systems. (dry--randomaccessfile, like InputStream and OutputStream classes, both inherit the Datainput and DataOutput interfaces, But there is no relationship between the Randomaccessfile class and the latter .
- In fact, in addition to implementing the Datainput and DataOutput interfaces (both DataInputStream and DataOutputStream implement both interfaces), it has nothing to do with these two classes, Do not even use any of the features that already exist in the InputStream and OutputStream classes; it is a completely independent class, and all methods (most of them belong to itself) are written from scratch. This may be because randomaccessfile can move back and forth within a file, so its behavior is fundamentally different from other I/O classes. In summary, it is a separate class that inherits directly from object.
1.3) Basically, the way Randomaccessfile works is to combine DataInputStream and dataoutputstream, plus some of its own methods.
- For example, the location of the Getfilepointer (), in the file to move the Seek (), and to determine the size of the file length (), skipbytes () skipped how many bytes. In addition, its constructor has a parameter that indicates whether to open the file as read-only ("R") or read-write ("RW") (fopen () of C). It does not support write-only files. (dry--randomaccessfile does not support write-only files, but supports R and RW access modes)
1.4) Only Randomaccessfile has a seek search method, and this method applies only to files.
- Bufferedinputstream has a mark () method that you can use to set the tag (save the result in an internal variable), and then call Reset () to return to that position, but it is too weak and not practical. (the function of seek in dry--randomaccessfile is equivalent to the combination of the Mark method and the Reset method of the Bufferedinputstream Class)
1.5) Most of the features of Randomaccessfile, but not all, have been superseded by the "Memory-mapped files (memory-mapped file)" of the JDK 1.4 NiO.
- You should consider whether to use "memory mapped files" instead of Randomaccessfile. (most of the functions of dry--randomaccessfile are replaced by NIO's "Memory Mapping Files" (memory-mapped file), consider using memory-mapped files instead of Randomaccessfile)
"2" Memory-mapped file
2.1) Memory-mapped files allow you to create and modify files that are too large to fit into memory. (Dry goods-the role of memory-mapped files)
- With a memory-mapped file, you can assume that the file has all been read into memory, and then it is accessed as a very large array. This solution greatly simplifies the code to modify the file.
Filechannel.map (Filechannel.mapmode mode, long position, long size) maps the file area of this channel directly into memory.
- Attention) Note that you have to indicate where it starts mapping from where the file is, how big the map is, and that is, it can also map a small fragment of a large file.
2.2) Mappedbytebuffer is a subclass of Bytebuffer: so it has all the methods of Bytebuffer, but the new force () forces the contents of the buffer to be flushed to the storage device, load () Whether data in the storage device is loaded into in-memory, isLoaded () location memory is synchronized with the storage settings. In addition to the put () and get () methods, you can easily read and write basic type data by using methods such as Ascharbuffer () to get a buffered view of the corresponding basic type of data.
2.3) Although mapping writes seem to use FileOutputStream, all output from the mapping file must use Randomaccessfile.
- But if you only need to read when you can use FileInputStream, write the map file must use random access to the file, may write to read the reason for it. (Dry goods-you can use FileInputStream when you need to read, you must use random access files when writing map files)
2.4) The program creates a 128Mb file that can cause memory overflow if read to memory at once, but the access here seems like just a moment, because the real memory is only a small part of it, and the rest is placed on the swap file. This makes it easy for you to modify very large files (up to 2 GB).
- Attention) Note that Java is called the "file mapping mechanism" of the operating system to improve performance.
"3" Look Litchi
- Attention) for source code, please visit https://github.com/pacosonTang/core-java-volume/blob/master/ Corejavaadvanced/chapter1/randomaccessfiledemo.java
3.1) Application of Randomaccessfile class:
3.2) Randomaccessfile Insert Write Example:
3.3) Multi-threaded download of files using Randomaccessfile:
- When you download a file in multiple threads, you divide the file into chunks, each of which is downloaded with a different thread. Here is an example of using multithreading when writing a file, where pre-allocating the space required for a file, then chunking in the allocated space, and then writing:
Randomaccessfile and memory-mapped files