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

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

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

第 1 组,第 2 组

io.RandomAccessFile 类的更多方法:

  1. write(int bytes) : Java.io.RandomAccessFile.write(int bytes)将指定的字节写入文件,从当前文件指针开始。
    Syntax : 
    public void write(int bytes)
    Parameters :
    bytes : bytes to be written 
    Return :
    -----
    Exception :
    IOException :in case an I/O error occurs.
    
  2. write(byte[] b) : Java.io.RandomAccessFile.write(byte[] b)将 b.length 个字节从特定字节数组写入文件,从当前文件指针开始。
    Syntax : 
    public void write(byte[] b)
    Parameters :
    b : data to be written
    Return :
    -------
    Exception :
    IOException :in case an I/O error occurs.
    
  3. write(byte[] b, int offset, int len) : Java.io.RandomAccessFile.write(byte[] b, int offset, int len)从缓冲区写入从偏移位置初始化到 b.length 的字节。
    Syntax : 
    public int write(byte[] b, int offset, int len)
    Parameters :
    b : buffer to write
    offset : starting position to write
    len : max no of bytes to write
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  4. writeBoolean(boolean b) : Java.io.RandomAccessFile.writeBoolean(boolean b)
    将布尔值作为单字节值写入文件。写入 True 是值为 '1' 否则为 false
    Syntax : 
    public final void writeBoolean(boolean b)
    Parameters :
    b : boolean value to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  5. writeByte(int b) : Java.io.RandomAccessFile.writeByte(int b)
    writes a byte to file as one-byte value, starting from the current position.
    Syntax : 
    public final void writeByte(int b)
    Parameters :
    b : byte to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  6. writeShort(int b) : Java.io.RandomAccessFile.writeShort(int b)从当前位置开始以两字节值的形式向文件写入一个short。
    Syntax : 
    public final void writeShort(int b)
    Parameters :
    b : short to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  7. writeChar(int c) : Java.io.RandomAccessFile.writeChar(int c)从当前位置开始将 char 作为两字节值写入文件。
    Syntax : 
    public final void writeChar(int c)
    Parameters :
    c : char to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  8. writeInt(int i) : Java.io.RandomAccessFile.writeInt(int i)从当前位置开始将 int 作为四字节值写入文件。
    Syntax : 
    public final void writeInt(int i)
    Parameters :
    i : int value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  9. writeLong(long l) : Java.io.RandomAccessFile.writeLong(long l)从当前位置开始将 int 作为 8 字节值写入文件。
    Syntax : 
    public final void writeLong(long l)
    Parameters :
    l : long value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  10. writeFloat(float f) : Java.io.RandomAccessFile.writeFloat(float f)将 float 参数转换为 int,然后从当前位置开始将 int 作为四字节值写入文件。
    Syntax : 
    public final void writeFloat(float f)
    Parameters :
    f : float value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  11. writeDouble(双 d): Java.io.RandomAccessFile.writeDouble(双 d)
    将 double 参数转换为 long,然后将 int 作为 8 字节值写入文件,从当前位置开始。
    Syntax : 
    public final void writeDouble(double d)
    Parameters :
    d : double value to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  12. writeBytes(String s) : Java.io.RandomAccessFile.writeBytes(String s)将字符串作为字节序列写入文件。
    Syntax : 
    public final void writeBytes(String s)
    Parameters :
    s : byte string to be written
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
  13. writeUTF(String str) : Java.io.RandomAccessFile.writeUTF(String str)使用修改后的 UTF-8编码将字符串写入文件(与机器无关)
    Syntax : 
    public final void writeUTF(String str)
    Parameters :
    str : String to be written 
    Return :
    ---------
    Exception :
    IOException :in case an I/O error occurs.
    
    // Java Program illustrating use of io.RandomAccessFile class methods
    // writeUTF(), writeChar(), writeDouble(), writeFloat(), write(byte[] b), 
    // write(int i), writeBoolean(), writeLong(), writeShort()
    // write(byte[] b, int offset, int len)
       
    import java.io.*;
    public class NewClass
    {
        public static void main(String[] args)
        {
            try
            {
                long l = 458754545576l;
                double d = 1.5;
                float f = 14.56f;
                int i = 1;
                boolean bol = true;
                short s = 15; 
       
                // Creating a new RandomAccessFile - "GEEK"
                RandomAccessFile geek = new RandomAccessFile("GEEK.txt", "rw");
       
                // writeUTF() : 
                geek.writeUTF("Hello Geeks For Geeks");
                  
                geek.seek(0);
                System.out.println("writeUTF : " + geek.readUTF()); 
                geek.seek(0);
       
                byte[] b = {1, 2, 3, 6, 5, 4};
                  
                // write(byte[] b) : 
                geek.write(b);
                  
                geek.seek(0);
                System.out.println("Use of .read(byte[] b) : " + geek.read(b));
                  
                // write(int i) :  
                geek.write(i);
                
                geek.seek(0);
                System.out.println("write(int i) : " + geek.read(b));
                  
                // writeBoolean() : 
                geek.writeBoolean(bol);            
                  
                geek.seek(0);
                System.out.println("writeBoolean() : " + geek.readBoolean());
       
                geek.write(b, 2, 2);
                geek.seek(0);
          System.out.println("write(byte[] b, int offset, int len) : " + geek.readByte());
       
                // writeChar() : 
                geek.writeChar('c');
                  
                geek.seek(0);
                System.out.println("writeChar() : " + geek.readChar()); 
                geek.seek(0);
                  
                // writeDouble() : 
                geek.writeDouble(d);
                  
                geek.seek(0);            
                System.out.println("writeDouble() : " + geek.readDouble()); 
                geek.seek(0);
                  
                //writeFloat() : 
                geek.writeFloat(f);
                  
                geek.seek(0);
                System.out.println("writeFloat() : " + geek.readFloat()); 
                  
                // writeLong() :
                geek.writeLong(l);
                  
                geek.seek(0);
                System.out.println("writeLong() : " + geek.readLong()); 
                  
                // writeShort() :
                geek.writeShort(s);
                  
                geek.seek(0);
                System.out.println("writeShort() : " + geek.readShort());  
                         
            }
            catch (IOException ex)
            {
                System.out.println("Something went Wrong");
                ex.printStackTrace();
            }
        }
    }
    

    输出 :

    writeUTF : Hello Geeks For Geeks
    Use of .read(byte[] b) : 6
    write(int i) : 6
    writeBoolean() : true
    write(byte[] b, int offset, int len) : 1
    writeChar() : c
    writeDouble() : 1.5
    writeFloat() : 14.56
    writeLong() : 4713287227910652010
    writeShort() : 16744