java io學習(二十五) RandomAccessFile

來源:互聯網
上載者:User

RandomAccessFile

RandomAccessFile 是隨機訪問檔案(包括讀/寫)的類。它支援對檔案隨機訪問的讀取和寫入,即我們可以從指定的位置讀取/寫入檔案資料。

需要注意的是,RandomAccessFile 雖然屬於java.io包,但它不是InputStream或者OutputStream的子類;它也不同於FileInputStream和FileOutputStream。 FileInputStream 只能對檔案進行讀操作,而FileOutputStream 只能對檔案進行寫操作;但是,RandomAccessFile 同時支援檔案的讀和寫,並且它支援隨機訪問。

RandomAccessFile 函數列表

RandomAccessFile(File file, String mode)RandomAccessFile(String fileName, String mode)     void     close()synchronized final FileChannel     getChannel()final FileDescriptor     getFD()long     getFilePointer()long     length()int     read(byte[] buffer, int byteOffset, int byteCount)int     read(byte[] buffer)int     read()final boolean     readBoolean()final byte     readByte()final char     readChar()final double     readDouble()final float     readFloat()final void     readFully(byte[] dst)final void     readFully(byte[] dst, int offset, int byteCount)final int     readInt()final String     readLine()final long     readLong()final short     readShort()final String     readUTF()final int     readUnsignedByte()final int     readUnsignedShort()void     seek(long offset)void     setLength(long newLength)int     skipBytes(int count)void     write(int oneByte)void     write(byte[] buffer, int byteOffset, int byteCount)void     write(byte[] buffer)final void     writeBoolean(boolean val)final void     writeByte(int val)final void     writeBytes(String str)final void     writeChar(int val)final void     writeChars(String str)final void     writeDouble(double val)final void     writeFloat(float val)final void     writeInt(int val)final void     writeLong(long val)final void     writeShort(int val)final void     writeUTF(String str)

1. RandomAccessFile 模式說明

RandomAccessFile共有4種模式:"r", "rw", "rws"和"rwd"。

"r"    以唯讀方式開啟。調用結果對象的任何 write 方法都將導致拋出 IOException。  

"rw"   開啟以便讀取和寫入。

"rws"  開啟以便讀取和寫入。相對於 "rw","rws" 還要求對“檔案的內容”或“中繼資料”的每個更新都同步寫入到基礎存放裝置。  

"rwd"  開啟以便讀取和寫入,相對於 "rw","rwd" 還要求對“檔案的內容”的每個更新都同步寫入到基礎存放裝置。  

說明:

(01) 什麼是“中繼資料”,即metadata?

英文解釋如下:

The definition of metadata is "data about other data." With a file system, the data is contained in its files and directories, and the metadata tracks information about each of these objects: Is it a regular file, a directory, or a link? What is its size, creation date, last modified date, file owner, group owner, and access permissions?

大致意思是:

metadata是“關於資料的資料”。在檔案系統中,資料被包含在檔案和檔案夾中;metadata資訊包括:“資料是一個檔案,一個目錄還是一個連結”,“資料的建立時間(簡稱ctime)”,“最後一次修改時間(簡稱mtime)”,“資料擁有者”,“資料擁有群組”,“存取權限”等等。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.