zoukankan      html  css  js  c++  java
  • java.io.RandomAccessFile

                                   RandomAccessFile

    1、RandomAccessFile构造方法

    RandomAccessFile(File file, String mode) throws FileNotFoundException
              
    创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。
    RandomAccessFile(String name, String mode) throws FileNotFoundException
              
    创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
    "r" 只读模式
    "w" 只写模式
    "rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件

     

    2、RandomAccessFile的常用方法  

    部分方法的的示例代码

    a)、

          View Code
     1 import java.io.*;
    2 public class TestRandomAccessFile1{
    3 public static void main(String[] args){
    4 File file = null;
    5 RandomAccessFile raf = null;
    6 String name = null ;
    7 int no = 0 ;
    8 try{
    9 file = new File("F:"+File.separator+"java"+File.separator+"IO"+File.separator+"RandomAccessFile"+File.separator+"TestRandomAccessFile.txt");
    10 //读写模式,如果文件不存在,会自动创建
    11 raf = new RandomAccessFile(file,"rw");
    12 name = "bchxsx " ; // 字符串长度为8
    13 no = 1 ; // 数字的长度为4
    14 raf.writeInt(no) ; // 将学号写入文件之中
    15 raf.writeBytes(name) ; // 将姓名写入文件之中
    16 name = "zhx " ; // 字符串长度为8
    17 no = 2 ; // 数字的长度为4
    18 raf.writeInt(no) ; // 将学号写入文件之中
    19 raf.writeBytes(name) ; // 将姓名写入文件之中
    20 name = "zj " ; // 字符串长度为8
    21 no = 3 ; // 数字的长度为4
    22 raf.writeInt(no) ; // 将学号写入文件之中
    23 raf.writeBytes(name) ; // 将姓名写入文件之中
    24 }catch(FileNotFoundException e){
    25 e.printStackTrace();
    26 }catch(IOException e){
    27 e.printStackTrace();
    28 }finally{
    29 try{
    30 if(raf!=null)
    31 raf.close(); // 关闭
    32 }catch(IOException e){
    33 e.printStackTrace();
    34 }
    35 }
    36 }
    37 }

    b)、

          View Code
     1 import java.io.File ;
    2 import java.io.RandomAccessFile ;
    3 public class TestRandomAccessFile2{
    4 // 所有的异常直接抛出,程序中不再进行处理
    5 public static void main(String args[]) throws Exception{
    6 File file = new File("F:"+File.separator+"java"+File.separator +"IO"+File.separator+"RandomAccessFile"+File.separator
    7 +"TestRandomAccessFile.txt") ; // 指定要操作的文件
    8 // 声明RandomAccessFile类的对象
    9 RandomAccessFile raf = null ;
    10 // 以只读的方式打开文件
    11 raf = new RandomAccessFile(file,"r") ;
    12 String name = null ;
    13 int no = 0 ;
    14 byte b[] = new byte[8] ; // 开辟byte数组
    15
    16 // 读取第二个人的信息,意味着要空出第一个人的信息
    17 raf.skipBytes(12) ; // 跳过第一个人的信息
    18 for(int i=0;i<b.length;i++){
    19 b[i] = raf.readByte() ; // 读取一个字节
    20 }
    21 name = new String(b) ;// 将读取出来的byte数组变为字符串
    22 no = raf.readInt() ; // 读取数字
    23 System.out.println("第二个人的学号:" + no +"姓名:" + name ) ;
    24
    25
    26 // 读取第一个人的信息
    27 raf.seek(0) ; // 指针回到文件的开头
    28 for(int i=0;i<b.length;i++){
    29 b[i] = raf.readByte() ; // 读取一个字节
    30 }
    31 name = new String(b) ;// 将读取出来的byte数组变为字符串
    32 no = raf.readInt() ; // 读取数字
    33 System.out.println("第一个人的学号:" + no +"姓名:" + name ) ;
    34
    35
    36 raf.skipBytes(12) ; // 空出第二个人的信息
    37 for(int i=0;i<b.length;i++){
    38 b[i] = raf.readByte() ; // 读取一个字节
    39 }
    40 name = new String(b) ;// 将读取出来的byte数组变为字符串
    41 no = raf.readInt() ; // 读取数字
    42 System.out.println("第三个人的学号:" + no +"姓名:" + name ) ;
    43 raf.close() ; // 关闭
    44 }
    45 }




    RandomAccessFile

    1、RandomAccessFile构造方法

    RandomAccessFile(File file, String mode) throws FileNotFoundException
              
    创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。

    RandomAccessFile(String name, String mode) throws FileNotFoundException
              
    创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。

     

    "r"

    只读模式

     

    "rw"

    打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。

     

    "r"

    只写模式

     

     

    2、RandomAccessFile的常用方法

    close()关闭此随机访问文件流并释放与该流关联的所有系统资源。

    length() 返回此文件的长度。

    read()  从此文件中读取一个数据字节。

    read(byte[] b) 将最多b.length个数据字节从此文件读入 byte 数组。

    read(byte[] b, int off, int len) 将最多len个数据字节从此文件读入 byte 数组。

    readBoolean() 从此文件读取一个boolean

    readByte() 从此文件读取一个有符号的八位值。

    readChar() 从此文件读取一个字符。

    readDouble()  从此文件读取一个double

    readFloat()  从此文件读取一个float

    readInt() 从此文件读取一个有符号的 32 位整数。

    readLine() 从此文件读取文本的下一行。

    readLong() 从此文件读取一个有符号的 64 位整数。

    readShort()   从此文件读取一个有符号的 16 位数。

    seek(long pos)   设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作

    setLength(long newLength)  设置此文件的长度。

    skipBytes(int n)尝试跳过输入的n个字节以丢弃跳过的字节。

    write(byte[] b)   b.length个字节从指定 byte 数组写入到此文件,并从当前文件指针开始。

    write(byte[] b, int off, int len)  len个字节从指定 byte 数组写入到此文件,并从偏移量off处开始。

    write(int b)  向此文件写入指定的字节。

    writeBoolean(boolean v)按单字节值将boolean写入该文件。

    writeByte(int v)按单字节值将byte写入该文件。

    writeChar(int v) 按双字节值将char写入该文件,先写高字节。

    writeChars(String s)   按字符序列将一个字符串写入该文件。

    writeDouble(double v)使用Double类中的doubleToLongBits方法将双精度参数转换为一个long,然后按八字节数量将该long值写入该文件,先定高字节。

    writeFloat(float v)使用Float类中的floatToIntBits方法将浮点参数转换为一个int,然后按四字节数量将该int值写入该文件,先写高字节。

    writeInt(int v)  按四个字节将int写入该文件,先写高字节

    writeLong(long v)  按八个字节将long写入该文件,先写高字节。

    writeShort(int v) 按两个字节将short写入该文件,先写高字节。

     

  • 相关阅读:
    drf中ListSerializer源码
    drf中get/post/delete/put/patch五大接口
    drf三大认证组件
    drf序列化与反序列化,基表的概念
    drf序列化与反序列化
    drf解析,异常,响应模块
    drf 简介以及部分源码分析
    vue学习第四天
    vue学习第三天
    MySQLStudy——索引
  • 原文地址:https://www.cnblogs.com/bchxsx322/p/2417679.html
Copyright © 2011-2022 走看看