zoukankan      html  css  js  c++  java
  • java.io.RandomAccessFile的使用,指定位置插入字符串(覆盖插入)。jdk1.4增加内存映射文件FileChannel、MappedByteBuffer

       //目的是在java文件中插入属性和getter、setter方法 
    //找到最后一个}的结束位置,插入内容,并把}补上,}的ascii码是125
        RandomAccessFile randomAccessFile = null;
    try {
    Field[] fields_beihai = com.supermap.realestate.registration.model.genrt.GenerateBDCS_TDYT_GZ.class.getDeclaredFields();

    Field[] fields = com.supermap.realestate.registration.model.genrt.GenerateBDCS_TDYT_GZ.class.getDeclaredFields();

    // Class cls = com.supermap.realestate.registration.model.genrt.GenerateBDCS_TDYT_GZ.class;

    Class cls = Test.class;

    String modelName = cls.getSimpleName();
    String packagepath = cls.getPackage().getName().replaceAll("\.","/");
    String root = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    root = root.replace("target/classes", "src/main/java");
    System.out.println(root + packagepath);
    File file = new File(root + packagepath);
    if(!file.isDirectory()) return;
    File[] files = file.listFiles();
    File modelFileResult = null;
    for (File modelFile : files) {
    String model = modelFile.getName();
    if(!model.contains(".java")) continue;
    if(!model.contains(modelName)) continue;
    modelFileResult = modelFile;
    break;
    }
    if(modelFileResult == null) return;
    System.out.println(modelFileResult.getAbsolutePath());

    randomAccessFile = new RandomAccessFile(modelFileResult,"rw");
    byte[] bytes = new byte[100];
    int len = 0;
    String aa = "";
    long start = randomAccessFile.getFilePointer();
    System.out.println("point=" + start);
    long total = randomAccessFile.length();
    System.out.println("total=" + randomAccessFile.length());

    int lastLen = 1;

    randomAccessFile.seek(total - lastLen); //从最后一个字符读取,判断是不是},是就插入新的内容
    byte lastChar = 0;
            // }的ascii码是125,找到最后一个}的位置,插入内容
                while((total - lastLen)>=0 && (lastChar = randomAccessFile.readByte()) != 125){
    System.out.println(lastLen++);
    randomAccessFile.seek(total - lastLen);
    }
    System.out.println(new String(new byte[]{lastChar}));
    start = total - lastLen;
    System.out.println("point=" + start);

    randomAccessFile.seek(start);
    randomAccessFile.write(" private String id; public void setId(String id){ this.id = id; } }".getBytes());



    } catch (Exception e) {
    e.printStackTrace();
    }finally {
    try {
    if(randomAccessFile != null ) randomAccessFile.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    内存映射文件:

    import java.io.RandomAccessFile;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;

    public class LargeMappedFiles {
    static int length = 0x8000000; // 128 Mb

    public static void main(String[] args) throws Exception {
    // 为了以可读可写的方式打开文件,这里使用RandomAccessFile来创建文件。
    FileChannel fc = new RandomAccessFile("test.dat", "rw").getChannel();
    //注意,文件通道的可读可写要建立在文件流本身可读写的基础之上
    MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);
    //写128M的内容
    for (int i = 0; i < length; i++) {
    out.put((byte) 'x');
    }
    System.out.println("Finished writing");
    //读取文件中间6个字节内容
    for (int i = length / 2; i < length / 2 + 6; i++) {
    System.out.print((char) out.get(i));
    }
    fc.close();
    }
    }

  • 相关阅读:
    路飞学城-Python开发集训-第1章
    Python制作的射击游戏
    使用百度地图API自动获取地址和经纬度
    使用高德地图JS获取当前位置和经纬度
    thinkphp5 使用PHPExcel 导入导出
    MySQL优化
    ABAP 新语法-实例讲解
    ASP.NET Core
    ASP.NET Core
    ASP.NET Core
  • 原文地址:https://www.cnblogs.com/shihx/p/13458212.html
Copyright © 2011-2022 走看看