【基礎篇】java中輸入輸出的總括—— 隨機檔案操作

來源:互聯網
上載者:User

不想把別人的東西佔為己有,但是想方便日後參考還是摘錄了。

煩死了,看Java編程思想三或者四,感覺老外寫書跟我們看書的習慣都不一樣的,總感覺老外寫的東西就像是在寫手冊,全面但是煩瑣。

【原則】不要告訴我曆史,告訴我怎麼做就行了。

【事實】輸出輸入類,就是TMD的簡單,為什麼非要弄成手冊,讓我這個菜鳥看不懂

【鳴謝】中國IT實驗室的總結篇

 —————————————————————————————————————————————————————————— 

隨機檔案操作

於InputStream 和OutputStream 來說,它們的執行個體都是順序訪問流,也就是說,只能對檔案進行順序地讀/寫。隨機訪問檔案則允許對檔案內容進行隨機讀/寫。在java中,類RandomAccessFile 提供了隨機訪問檔案的方法。類RandomAccessFile的聲明為:
public class RandomAccessFile extends Object implements DataInput, DataOutput
     File:以檔案路徑名的形式代表一個檔案
     FileDescriptor:代表一個開啟檔案的檔案描述
     FileFilter & FilenameFilter:用於列出滿足條件的檔案
     File.list(FilenameFilter fnf)
     File.listFiles(FileFilter ff)
     FileDialog.setFilenameFilter(FilenameFilter fnf)
•     FileInputStream & FileReader:順序讀檔案
•     FileOutputStream & FileWriter:順序寫檔案
•     RandomAccessFile:提供對檔案的隨機訪問支援
類RandomAccessFile則允許對檔案內容同時完成讀和寫操作,它直接繼承Object,並且同時實現了介面DataInput和DataOutput,提供了支援隨機檔案操作的方法
     DataInput和DataOutput中的方法
•     readInt(), writeDouble()…
     int skipBytes(int n):將指標鄉下移動若干位元組
     length():返迴文件長度
     long getFilePointer():返回指標當前位置
     void seek(long pos):將指標調到所需位置
     void setLength(long newLength):設定檔案長度
構造方法:
RandomAccessFile(File file, String mode)
     RandomAccessFile(String name, String mode)
mode 的取值
–     “r” 唯讀. 任何寫操作都將拋出IOException。
–     “rw” 讀寫. 檔案不存在時會建立該檔案,檔案存在時,原檔案內容不變,通過寫操作改變檔案內容。
–     “rws” 同步讀寫. 等同於讀寫,但是任何協操作的內容都被直接寫入物理檔案,包括檔案內容和檔案屬性。
–     “rwd” 資料同步讀寫. 等同於讀寫,但任何內容寫操作都直接寫到物理檔案,對檔案屬性內容的修改不是這樣。
例 8.6 隨機檔案操作。
本例對一個二進位整數檔案實現訪問操作當以可讀寫方式“rw“開啟一個檔案”prinmes.bin“時,如果檔案不存在,將建立一個新檔案。先將2作為最小素數寫入檔案,再依次測試100以內的奇數,將每次產生一個素數寫入檔案尾。
程式如下:
import java.io.*;
public class PrimesFile
{
RandomAccessFile raf;
public static void main(String args[]) throws IOException
{
(new PrimesFile()). createprime(100);
}
public void createprime(int max) throws IOException
{
raf=new RandomAccessFile("primes.bin","rw");//建立檔案對象
raf.seek(0); //檔案指標為0
raf.writeInt(2); //寫入整型
int k=3;
while (k<=max)
{
if (isPrime(k))
raf.writeInt(k);
k = k+2;
}
output(max);
raf.close(); //關閉檔案
}
public boolean isPrime(int k) throws IOException
{
int i=0,j;
boolean yes = true;
try
{
raf.seek(0);
int count = (int)(raf.length()/4); //返迴文件位元組長度
while ((i<=count) && yes)
{
if (k % raf.readInt()==0) //讀取整型
yes = false;
else
i++;
raf.seek(i*4); //移動檔案指標
}
} catch(EOFException e) { } //捕獲到達檔案尾異常
return yes;
}
public void output(int max) throws IOException
{
try
{
raf.seek(0);
System.out.println("[2.."+max+"]中有 "+
(raf.length()/4)+" 個素數:");
for (int i=0;i<(int)(raf.length()/4);i++)
{
raf.seek(i*4);
System.out.print(raf.readInt()+" ");
if ((i+1)%10==0) System.out.println();
}
} catch(EOFException e) { }
System.out.println();
}
}
程式運行時建立檔案“primes.bin“,並將素數寫入其中,結果如下:
[2..100]中有 25 個素數:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.