RandomAccessFile类

/*
   * r代表以可读方式打开。
   * rw代表以可写的方式打开。
   */

读取文件

 public static void randomRead(File file, int pointer){
  
  RandomAccessFile randomAccessFile = null;
  try {
   
    randomAccessFile = new RandomAccessFile(file, "r");
   randomAccessFile.seek(pointer);
   byte[]bytes = new byte[1024];
   int hasRead = 0;
   while((hasRead = randomAccessFile.read(bytes)) != -1){
    System.out.println(new String(bytes,0,hasRead));
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if (randomAccessFile != null) {
    try {
     randomAccessFile.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
 /*
  * 写文件
  */
 public static void randomWrite(File file,String appendContent,long seek){
  RandomAccessFile randomAccessFile = null;
  try {
   randomAccessFile = new RandomAccessFile(file,"rw");
   randomAccessFile.seek(randomAccessFile.length());
   randomAccessFile.write(appendContent.getBytes());
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 /*
  *
  */

插入文件
 public static void insertByRandomAccessFile(File file, long pointer,String insertContent){
  try {
   File tempFile = File.createTempFile("temp", null);//创建一个新的文件存放。
   tempFile.deleteOnExit();
   
   RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
   
   FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
   FileInputStream fileInputStream = new FileInputStream(tempFile);
   //用来读写新的文件。
   
   randomAccessFile.seek(pointer);//设置指针偏移量
   
   System.out.println(randomAccessFile.getFilePointer());//打印出第一次指针的位置。
   byte[] bytes = new byte[1024];
   int hasRead = 0;
   while((hasRead = randomAccessFile.read(bytes)) != -1){
    fileOutputStream.write(bytes, 0, hasRead);
   //从第一个偏移量开始,并写入流中,存入到新的文件中。
   }
   randomAccessFile.seek(pointer);//随着while循环,偏移量也会变化,需要重新回到之前的位置。
   
   fileOutputStream.flush();
   
   //从此时写入要插入的数据内容覆盖写入源文件中。
   randomAccessFile.write(insertContent.getBytes());
   
   //而此时偏移量也到了写入内容的后面。
   
    while((hasRead = fileInputStream.read(bytes)) != -1){
     randomAccessFile.write(bytes, 0, hasRead);
     // 这时将之前从指定偏移量之后复制的新文件,并在此覆盖的写入此时文件偏移量开始之处。
    }
    
    randomAccessFile.close();
    fileOutputStream.close();
    fileInputStream.close();
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值