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

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

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

第 1 组,第 3 组
Java.io.RandomAccessFile 类方法的方法:

  1. readLine() : Java.io.RandomAccessFile.readLine()从此文件中读取下一行文本,从文件指针开始读取直到文件末尾。
    Syntax : 
    public final String readLine()
    Parameters : 
    -----
    Return : 
    reads the next line of text from this file
    
  2. readUnsignedByte() : Java.io.RandomAccessFile.readUnsignedByte()从文件中读取一个无符号的 8 位数字,从当前文件指针开始读取。
    Syntax : 
    public final int readUnsignedByte()
    Parameters : 
    ------
    Return : 
    reads an unsigned 8 bit number from file
    
  3. readUnsignedShort() : Java.io.RandomAccessFile.readUnsignedShort()从文件中读取一个无符号的 16 位数字,从当前文件指针开始读取。
    Syntax : 
    public final int readUnsignedShort()
    Parameters : 
    -------
    Return : 
    reads an unsigned 16 bit number from file
    
  4. readUTF( ) : Java.io.RandomAccessFile.readUTF()从文件中读取字符串
    Syntax : 
    public final String readUTF()
    Parameters : 
    -------
    Return : 
    Unicode String
    
  5. seek(long pos) : Java.io.RandomAccessFile.seek(long pos)设置文件指针位置。
    Syntax : 
    public void seek(long pos)
    Parameters : 
    pos : start position from file, measured in bytes
    Return : 
    --------
    
  6. setLength(long len) : Java.io.RandomAccessFile.setLength(long len)文件的长度。
    Syntax : 
    public void setLength(long len)
    Parameters : 
    len : desired length of the file
    Return : 
    -------
    
  7. skipBytes(int n) : Java.io.RandomAccessFile.skipBytes(int n)跳过 n 个字节,丢弃跳过的字节
    Syntax : 
    public int skipBytes(int n)
    Parameters : 
    n : no. of bytes to be skipped
    Return : 
    no. of bytes skipped
    
  8. getChannel() : Java.io.RandomAccessFile.getChannel()返回与文件关联的唯一 FileChannel 对象。
    Syntax : 
    public final FileChannel getChannel()
    Parameters : 
    ------
    Return : 
    returns unique FileChannel object
    
  9. Java.io.RandomAccessFile.length() :返回文件的长度。
    Syntax : 
    public long length()
    Parameters : 
    ----
    Return : 
    length of the file, measured in bytes
    
  10. getFilePointer() : Java.io.RandomAccessFile.getFilePointer()返回文件中的当前偏移量(以字节为单位)。
    Syntax : 
    public long getFilePointer()
    Parameters : 
    ----
    Return : 
    return current offset in the file in bytes
    
  11. getFD() : Java.io.RandomAccessFile.getFD()返回文件描述符对象和流。
    Syntax : 
    public final FileDescriptor getFD()
    Parameters : 
    -----------
    Return : 
    file descriptor object with the stream.
    
  12. close() : Java.io.RandomAccessFile.close()关闭随机访问文件流并释放与流关联的源(如果有)。
    Syntax : 
    public void close()
    Parameters : 
    -------
    Return : 
    -------
    
    // Java Program illustrating use of io.RandomAccessFile class methods
    // seek(), readLine(), readUTF(), readUnsignedByte(), readUnsignedShort(),
    // setLength(), length(), skipBytes(), getFilePointer(), getChannel(),
    // getFD(), close()
      
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            try
            {
                // Creating a new RandomAccessFile - "GEEK"
                RandomAccessFile geek = new RandomAccessFile("FILE.txt", "rw");
      
                // Writing to file
                geek.writeUTF("Hello Geeks For Geeks");
      
                geek.seek(0);
      
                // Use of readUTF() :
                System.out.println("Use of readUTF() : " + geek.readUTF());
      
                //Use of seek() :
                geek.seek(0);
      
                // Use of readLine() :
                System.out.println("1 readLine() : " + geek.readLine());
                geek.seek(0);
      
                geek.writeUTF("Hello \nGeeks For Geeks");
                geek.seek(0);
      
                System.out.println("2 readLine() : " + geek.readLine());
      
                geek.seek(3);
                // Use of readUnsignedByte() :
              System.out.println("Use of readUnsignedByte() :  " + geek.readUnsignedByte());
      
                geek.seek(4);
                // Use of readUnsignedShort() :
              System.out.println("Use of readUnsignedByte() :  " + geek.readUnsignedShort());
      
                // Use of setLength():
                geek.setLength(78);
      
                // Use of length() :
                System.out.println("Use of setLength() : " + geek.length());
      
                geek.seek(2);
                // Use of skipBytes() :
                System.out.println("Use of skipBytes() : " + geek.skipBytes(3));
      
      
                // Use of getFilePointer() :
                System.out.println("Use of getFilePointer() : " + geek.getFilePointer());
      
                // Use of getChannel() :
                System.out.println("Use of getChannel() : " + geek.getChannel());
      
                // Use of getFD() :
                System.out.println("Use of getFD() : " + geek.getFD());
      
                // Use of close() method :
                geek.close();
                System.out.println("Stream Closed.");
      
            }
            catch (IOException ex)
            {
                System.out.println("Something went Wrong");
                ex.printStackTrace();
            }
        }
    }
    

    输出 :

    Use of readUTF() : Hello Geeks For Geeks
    1 readLine() : Hello Geeks For Geekss
    2 readLine() : Hello
    Use of readUnsignedByte() :  101
    Use of readUnsignedByte() :  27756
    Use of setLength() : 78
    Use of skipBytes() : 3
    Use of getFilePointer() : 5
    Use of getChannel() : sun.nio.ch.FileChannelImpl@15db9742
    Use of getFD() : java.io.FileDescriptor@6d06d69c
    Stream Closed.