1, Random access file stream
1. Introduction
RandomAccessFile can be used as both an input stream and an output stream. As an output stream, if the file written out does not exist, it will be automatically created during execution. If it exists, the original file content will be overwritten. By default, it will be overwritten from the beginning. Note that the entire file content will not be overwritten.
To create an instance of RandomAccessFile class, you need to specify a mode parameter, which specifies its access mode:
r: open as read-only.
rw: open for reading and writing.
rwd: open for reading and writing; Synchronize updates to file contents.
rws: open for reading and writing; Synchronize file content and metadata updates.
If the mode is r, a file will not be created, but an existing file will be read. If the read file does not exist, an exception will occur.
If the mode is rw read / write, if the file does not exist, it will be created; if it does exist, it will not be created.
2,RandomAccessFile
Code example: copy picture
1 public class Main { 2 public static void main(String[] args) { 3 try (RandomAccessFile raf1 = new RandomAccessFile(new File("F:\\hello.png"), "r"); 4 RandomAccessFile raf2 = new RandomAccessFile(new File("F:\\hello_1.png"), "rw");) { 5 6 byte[] buffer = new byte[1024]; 7 int len; 8 9 while ((len = raf1.read(buffer)) != -1) { 10 raf2.write(buffer, 0, len); 11 } 12 } catch (Exception e) { 13 } 14 } 15 }
Code example: random write file
1 // File: F:\\hello.txt 2 // Content: abcdefgh 3 public class Main { 4 public static void main(String[] args) { 5 6 File file = new File("F:\\hello.txt"); 7 try (RandomAccessFile raf = new RandomAccessFile(file, "rw");) { 8 9 // raf1.seek(file.length()); // Can be added at the end of the implementation file 10 11 // Adjust the pointer to the position marked 3 12 raf.seek(3); 13 raf.write("xyz".getBytes()); 14 15 } catch (Exception e) { 16 } 17 } 18 } 19 20 // result 21 // abcxyzgh
Code example: achieve the effect of data insertion
1 // File: F:\\hello.txt 2 // Content: 3 abcdefghi 4 jkl 5 mn 6 public class Main { 7 public static void main(String[] args) { 8 File file = new File("F:\\hello.txt"); 9 10 try (RandomAccessFile raf = new RandomAccessFile(file, "rw");) { 11 // 1.Save the data behind the position to be inserted 12 raf.seek(3); 13 StringBuilder builder = new StringBuilder((int) file.length()); 14 15 // 2.Save the read data in builder in 16 byte[] buffer = new byte[1024]; 17 int len; 18 while ((len = raf.read(buffer)) != -1) { 19 builder.append(new String(buffer, 0, len)); 20 } 21 22 // 3.Call back the pointer and insert data“ xyz" 23 raf.seek(3); 24 raf.write("xyz".getBytes()); 25 26 // 4.take builder The data in is appended to the file 27 raf.write(builder.toString().getBytes()); 28 } catch (Exception e) { 29 } 30 } 31 } 32 33 // result 34 abcxyzdefghi 35 jkl 36 mn
Obviously, this method is very unfriendly for large text files and inserting in the front position.
3. Apply
RandomAccessFile can be used to realize a multi-threaded breakpoint download function. Friends who have used the download tool know that before downloading, two temporary files will be created, one is an empty file with the same size as the downloaded file, and the other is to record the file pointer position. Each pause will save the last pointer, Then, during breakpoint download, it will continue to download from the last place, so as to realize the function of breakpoint download or upload.