📜  Java.io.RandomAccessFile 类方法 |设置 1

📅  最后修改于: 2022-05-13 01:55:44.310000             🧑  作者: Mango

Java.io.RandomAccessFile 类方法 |设置 1

Java.io.RandomAccessFile类提供了一种使用读写操作随机访问文件的方法。它的工作方式类似于存储在文件中的字节数组。
宣言 :

public class RandomAccessFile
   extends Object
      implements DataOutput, DataInput, Closeable

RandomAccessFile 类的方法:

  1. read() : Java.io.RandomAccessFile.read()从文件中读取数据字节。字节以 0-255 范围内的整数形式返回
    Syntax :
    public int read()
    Parameters : 
    --------
    Return  :
    reads byte of data from file, -1 if end of file is reached.
    
  2. read(byte[] b) Java.io.RandomAccessFile.read(byte[] b)从缓冲区中读取最多 b.length 个字节。
    Syntax :
    public int read(byte[] b)
    Parameters : 
    b : buffer to be read
    Return  :
    byte of data from file upto b.length, -1 if end of file is reached.
    
  3. read((byte[] b, int offset, int len) : Java.io.RandomAccessFile.read((byte[] b, int offset, int len)从缓冲区读取从偏移位置初始化到 b.length 的字节。
    Syntax :
    public int read(byte[] b, int offset, int len)
    Parameters : 
    b : buffer to read
    offset : starting position to read
    len : max no of bytes to read
    Return  :
    reads bytes initialising from offset position upto b.length from the buffer.
    
  4. readBoolean() : Java.io.RandomAccessFile.readBoolean()从文件中读取一个布尔值。
    Syntax :
    public final boolean readBoolean()
    Parameters : 
    ------
    Return  :
    boolean value
    
  5. readByte() : Java.io.RandomAccessFile.readByte()从文件中读取一个有符号的八位值,从文件指针开始读取。
    Syntax :
    public final byte readByte()
    Parameters : 
    -------
    Return  :
    signed eight-bit value from file
    
  6. readChar() : Java.io.RandomAccessFile.readChar()从文件中读取一个字符,从文件指针开始读取。
    Syntax :
    public final char readChar()
    Parameters : 
    -------
    Return  :
    character from the file.
    
  7. readDouble() : Java.io.RandomAccessFile.readDouble()从文件中读取一个双精度值,从文件指针开始读取。
    Syntax :
    public final double readDouble()
    Parameters : 
    ------
    Return  :
    reads a double value from the file.
    
  8. readFloat() : Java.io.RandomAccessFile.readFloat()从文件中读取一个浮点值,从文件指针开始读取。
    Syntax :
    public final double readFloat()
    Parameters : 
    ------
    Return  :
    reads a float value from the file.
    
  9. readFully(byte[] b) : Java.io.RandomAccessFile.readFully(byte[] b)从缓冲区读取最多 b.length 的字节,从文件指针开始读取。
    Syntax :
    public final void readFully(byte[] b)
    Parameters : 
    b : buffer to be read
    Return  :
    reads bytes initialising from offset position upto b.length from the buffer
    
  10. readInt() : Java.io.RandomAccessFile.readInt()从文件中读取一个有符号的 4 字节整数,从文件指针开始读取。
    Syntax :
    reads a signed 4 bytes integer from the file
    Parameters : 
    -----
    Return  :
    reads a signed 4 bytes integer from the file
    
  11. readFully(byte[] b, int offset, int len) : Java.io.RandomAccessFile.readFully(byte[] b, int offset, int len)从缓冲区读取从偏移位置到 b.length 初始化的字节,从文件指针。
    Syntax :
    public final void readFully(byte[] b, int offset, int len)
    Parameters : 
    b : buffer to read
    offset : starting position to read
    len : max no of bytes to read
    Return  :
    bytes initialising from offset position upto b.length from the buffer
    
  12. readLong() : Java.io.RandomAccessFile.readLong()从文件中读取一个有符号的 64 位整数,从文件指针开始读取。
    Syntax :
    public final long readLong()
    Parameters : 
    -------
    Return  :
    signed 64 bit integer from the file
    
    // Java Program illustrating use of io.RandomAccessFile class methods
    // read(), read(byte[] b), readBoolean(), readByte(), readInt()
    // readFully(byte[] b, int off, int len), readFully(), readFloat()
    // readChar(), readDouble(),
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            try
            {
                double d = 1.5;
                float f = 14.56f;
      
                // Creating a new RandomAccessFile - "GEEK"
                RandomAccessFile geek = new RandomAccessFile("GEEK.txt", "rw");
      
                // Writing to file
                geek.writeUTF("Hello Geeks For Geeks");
      
                // File Pointer at index position - 0
                geek.seek(0);
      
                // read() method :
                System.out.println("Use of read() method : " + geek.read());
      
                geek.seek(0);
      
                byte[] b = {1, 2, 3};
                  
                // Use of .read(byte[] b) method :
                System.out.println("Use of .read(byte[] b) : " + geek.read(b));
      
                // readBoolean() method :
                System.out.println("Use of readBoolean() : " + geek.readBoolean());
      
                // readByte() method :
                System.out.println("Use of readByte() : " + geek.readByte());
      
                geek.writeChar('c');
                geek.seek(0);
      
                // readChar() :
                System.out.println("Use of readChar() : " + geek.readChar());
      
                geek.seek(0);
                geek.writeDouble(d);
                geek.seek(0);
      
                // read double
                System.out.println("Use of readDouble() : " + geek.readDouble());
      
                geek.seek(0);
                geek.writeFloat(f);
                geek.seek(0);
      
                // readFloat() :
                System.out.println("Use of readFloat() : " + geek.readFloat());
      
                geek.seek(0);
                // Create array upto geek.length
                byte[] arr = new byte[(int) geek.length()];
                // readFully() :
                geek.readFully(arr);
                  
                String str1 = new String(arr);
                System.out.println("Use of readFully() : " + str1);
      
                geek.seek(0);
                  
                // readFully(byte[] b, int off, int len) :
                geek.readFully(arr, 0, 8);
                  
                String str2 = new String(arr);
                System.out.println("Use of readFully(byte[] b, int off, int len) : " + str2);
            }
            catch (IOException ex)
            {
                System.out.println("Something went Wrong");
                ex.printStackTrace();
            }
        }
    }
    

    输出 :

    Use of read() method : 0
    Use of .read(byte[] b) : 3
    Use of readBoolean() : true
    Use of readByte() : 108
    Use of readChar() : c
    Use of readDouble() : 1.5
    Use of readFloat() : 14.56
    Use of readFully() : Geeks For Geeks
    Use of readFully(byte[] b, int off, int len) : Geeks For Geeks
    

    下一个:第 2 组,第 3 组