javaIO流實際操作文件內容,並倒序輸出文件內容

^_^關注點贊收藏,即可查看更多文章(所有文章中涉及的源碼都能免費分享,需要的私信留言)

javaIO流中經常讀取出文件然後需要對文件內容進行操作,RandomAccessFile類主要是對文件的隨機讀取功能,可以讀取文件的指定位置。

RandomAccessFile類常用的操作方法:

  1. public RandomAccessFile(File file, String mode)throws FileNotFoundException 構造方法 接收File類的對象,指定操作路徑,但是在設置時需要設置模式:"r": 只讀、"w": 只寫、"rw"("rws", or "rwd"): 讀寫。

  2. public RandomAccessFile(String name, String mode) throws FileNotFoundException 構造方法 不再使用File類對象表示文件,而是直接輸入了一個固定的文件路徑。

    Advertisements

  3. public void close() throws IOException 關閉操作

  4. public int read(byte[] b) throws IOException 將內容讀取到一個byte數組之中

  5. public final byte readByte() throws IOException 讀取一個位元組

  6. public final int readInt() throws IOException從文件中讀取整型數據。

  7. public void seek(long pos) throws IOException 設置讀指針的位置。

  8. public final void writeBytes(String s) throws IOException 將一個字元串寫入到文件之中,按位元組的方式處理。

    Advertisements

  9. public final void writeInt(int v) throws IOException 將一個int型數據寫入文件,長度為4位。

  10. public int skipBytes(int n) throws IOException 指針跳過多少個位元組。

構造方法有兩種:

  1. public RandomAccessFile(File file, String mode) throws FileNotFoundException

  2. public RandomAccessFile(String name, String mode) throws FileNotFoundException

源碼和實際運行結果如下圖:

源碼圖

源數據圖

控制台輸出結果圖

Advertisements

你可能會喜歡