Java learns from the past-RandomAccessFile

Document composition

Insert picture description here
Insert picture description here

RandomAccessFile common methods

Insert picture description here
Insert picture description here

Example: Realize random write

package com.itkey.javareview.温故知新.io;

import lombok.SneakyThrows;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

public class RandomAccessFileDemo {
    
    
    public static final int MAX_LENGTH =8;

    @SneakyThrows
    public static void main(String[] args) {
    
    
        File file = new File("/Users/itkey/Documents/GitHub/java-review/src/main/java/com/itkey/javareview/温故知新/io" + File.separator + "yootk.data");
        if(!file.getParentFile().exists()){
    
    
            file.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(file,"rw");

        String names[] = new String[] {
    
    "zhangsan","lisi","wangwu","zhaoliu","sunqi"};
        int ages[] = new int[] {
    
    12,13,17,16,15};
        int i = 0;
        for (String name : names) {
    
    
            name =  addSpace(name);
            raf.write(name.getBytes(StandardCharsets.UTF_8));
            raf.writeInt(ages[i]);
            i++;
        }
        raf.close();
    }

    /**
     * 字符过短,补空格
     * @param str
     * @return
     */
    public static String addSpace(String str){
    
    
        StringBuffer stringBuffer = new StringBuffer(str);
        while (stringBuffer.length()<MAX_LENGTH){
    
    
            stringBuffer.append(" ");
        }
        return stringBuffer.toString();
    }

}

Read binary random files

Insert picture description here

package com.itkey.javareview.温故知新.io;

import lombok.SneakyThrows;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

public class RandomAccessFile生成二进制文件 {
    
    
    public static final int MAX_LENGTH =8;

    @SneakyThrows
    public static void main(String[] args) {
    
    
        File file = new File("/Users/itkey/Documents/GitHub/java-review/src/main/java/com/itkey/javareview/温故知新/io" + File.separator + "yootk.data");
        if(!file.getParentFile().exists()){
    
    
            file.getParentFile().mkdirs();
        }
        RandomAccessFile raf = new RandomAccessFile(file,"rw");

        String names[] = new String[] {
    
    "zhangsan","lisi","wangwu","zhaoliu","sunqi"};
        int ages[] = new int[] {
    
    12,13,17,16,15};
        int i = 0;
        for (String name : names) {
    
    
            name =  addSpace(name);
            raf.write(name.getBytes(StandardCharsets.UTF_8));
            raf.writeInt(ages[i]);
            i++;
        }
        raf.close();
    }

    /**
     * 字符过短,补空格
     * @param str
     * @return
     */
    public static String addSpace(String str){
    
    
        StringBuffer stringBuffer = new StringBuffer(str);
        while (stringBuffer.length()<MAX_LENGTH){
    
    
            stringBuffer.append(" ");
        }
        return stringBuffer.toString();
    }

}

Insert picture description here

Guess you like

Origin blog.csdn.net/lxyoucan/article/details/114871968