【分享】Java輸入輸出流

【轉】Java輸入輸出流

  所 有 的 程 序 語 言 都 提 ?copy; 與 本 機 文 件 系 統 交 互 的 方式;Java也 不 例 外。 我 們 將 看 看Java是 怎 樣 處 理 標 準 文 件 輸 入輸 出 的(包 括stdin,stout,stderr)。 當 你 在 網 絡 上 開 發 小 程 序 時,你 必 須 注 意 直 接 文 件 輸 入 輸 出 是 不 安 全 因 素 的 關 鍵。 大多 數 用 戶 設 置 他 們 的 瀏 覽 器, 可 讓 你 自 由 的 訪 問 他 們 的文 件 系 統, 但 有 ?copy; 不 讓 你 訪 問。 當 然, 如 果 你 開 發 你內 部 的 應 用 程 序, 你 也 許 需 要 直 接 訪 問 文 件。

  標 準 輸 入 輸 出 Unix的 用 戶, 或 其 他 基 於 命 令 行 系 統 的 用戶(如DOS), 都 知 道 標 準 輸 入 輸 出 的 含 義。 標 準 輸 入 文 件 是鍵 盤, 標 準 輸 出 文 件 是 你 的 終 端 屏 幕。 標 準 錯 誤 輸 出 文 件也 指 向 屏 幕, 如 果 有 必 要, 它 也 可 以 指 向 另 一 個 文 件 以 便和 正 常 輸 出 區 分。

  系 統 類 Java通 過 系 統 類 達 到 訪 問 標 準 輸 入 輸 出 的 功 能。上 面 提 到 的 三 個 文 件 在 這 個 系 統 類 中 實 現: Stdin System.in作爲InputStream類 的 一 個 實 例 來 實 現stdin, 你 可 以 使 用read()和skip(long n)兩 個 成 員 函 數。read()讓 你 從 輸 入 中 讀 一 個 字 節,skip(long n)讓 你 在 輸 入 中 跳 過n個 字 節。

  Stout System.out作 爲PrintStream來 實 現stdout, 你 可 以 使 用print()和println()兩個 成 員 函 數。 這 兩 個 函 數 支 持Java的 任 意 基 本 類 型 作 爲 參數。

  Stderr System.err同stdout一 樣 實 現stderr。 象System.out一 樣,你 可 以 訪 問PrintStream 成 員 函 數。

  9.2 標 準 輸 入 輸 出 例 子

  這 裏 有 一 個 例 子, 功 能 象Unix裏 的cat或type:

  import java.io.* class myCat{ public void main(String args[]) throws IOException{ int b; int count = 0; while ((b = System.in.read()) != -1){ count++; System.out.print((char)b); } System.out.println(); //blank line System.err.println("counted"+count+"total bytes."); } }

  9.3 普 通 輸 入 輸 出 類

  除 了 基 本 的 鍵 盤 輸 入 和 屏 幕 輸 出 外, 我 們 還 需 要 聯 系文 件 的 輸 入 輸 出。 我 們 將 學 習 下 面 幾 個 類: l FileInputStream l DataInputStream l FileOutputStream l DataOutputStream

  作 爲 參 考, 再 列 出 一 ?copy; 特 定 應 用 的 類: l PipedInputStream l BufferedInputStream l PushBackInputStream l StreamTokenizer l PipedOutputStream l BufferedOutputStream l RandomAccessFile

  我 們 不 在 此 討 論 這 ?copy; 類, 但 你 可 以 在JAVA_HOME/src/java/io目錄 裏 查 看 每 個 類 的 成 員 函 數 定 義。

  9.4 文 件

  在 我 們 進 行 文 件 操 作 時, 需 要 知 道 一 ?copy; 關 於 文 件 的信 息。File類 提 ?copy; 了 一 ?copy; 成 員 函 數 來 操 縱 文 件 和 獲得 一 ?copy; 文 件 的 信 息。

  9.4.1 創 建 一 個 新 的 文 件 對 象

  你 可 用 下 面 三 個 方 法 來 創 建 一 個 新 文 件 對 象:

  File myFile; myFile = new File("etc/motd");

  或

  myFile = new File("/etc","motd"); //more useful if the directory or filename are variables

  或

  File myDir = new file("/etc"); myFile = new File(myDir,"motd");

  這 三 種 方 法 取 決 於 你 訪 問 文 件 的 方 式。 例 如, 如 果 你 在應 用 程 序 裏 只 用 一 個 文 件, 第 一 種 創 建 文 件 的 結 構 是 最容 易 的。 但 如 果 你 在 同 一 目 錄 裏 打 開 數 個 文 件, 則 第 二 種或 第 三 種 結 構 更 好 一 ?copy;。

  9.4.2 文 件 測 試 和 使 用

  一 ?copy; 你 創 建 了 一 個 文 件 對 象, 你 便 可 以 使 用 以 下 成員 函 數 來 獲 得 文 件 相 關 信 息:

  文 件 名 l String getName() l String getPath() l String getAbslutePath() l String getParent() l boolean renameTo(File newName)

  文 件 測 試 l boolean exists() l boolean canWrite() l boolean canRead() l boolean isFile() l boolean isDirectory() l boolean isAbsolute()

  一 般 文 件 信 息 l long lastModified() l long length()

  目 錄 用 法 l boolean mkdir() l String[] list()

  9.4.3 文 件 信 息 獲 取 例 子 程 序

  這 裏 是 一 個 獨 立 的 顯 示 文 件 的 基 本 信 息 的 程 序, 文 件通 過 命 令 行 參 數 傳 輸:

  import java.io.*; class fileInfo{ File fileToCheck; public static void main(String args[]) throws IOException{ if (args.length>0){ for (int i=0;i
  9.5 輸 入 流

  InputStream SequenceInputStream FileInputStream PipedInputStream ByteArrayInputStream FileterInputStream StringBufferInputStream

  DataInputStream LineNumberInputStream PushbackInputStream BufferedInputStream 有 好 幾 個 類 是 專 門 用 來 處 理 文 件 輸 入 的。 下 面 是 文 件 輸入 類 的 層 次 結 構:

  9.5.1 FileInputStream 對 象

  FileInputStream典 型 地 表 示 一 種 順 序 訪 問 的 文 本 文 件。 通過 使 用FileInputStream你 可 以 訪 問 文 件 的 一 個 字 節、 幾 個 字 節或 整 個 文 件。

  9.5.2 打 開FileInputStream

  爲 一 個 文 件 打 開 輸 入 流FileInputStream, 你 必 須 將 文 件 名或 文 件 對 象 傳 送 給 結 構:

  FileInput Stream myFileStream; myFileStream = new FileInputStream ( "/etc/motd");

  你 還 可 以 象 下 邊 這 樣 從FileInputStream裏 讀 文 件 信 息:

  File myFile ; FileInputSteam myFileStream; myFile = new File("/etc/motd"); myFileStream = new FileInputStream(myFile);

  一 ?copy;FileInputStream輸 入 流 打 開, 你 就 可 以 從 裏 面 讀 取信 息 了。read()成 員 函 數 有 以 下 幾 種 選 項:

  l int read() //reads one byte //return -1 at end of stream l int read(byte b[]) //fills entire array,if possible //returns number of bytes read //returns -1 if end of stream is reached

  l int read(byte b[],int offset, int len) //reads len bytes into b starting at b[offset] //Returns number of bytes read, //or -1 if end of stream is reached.

  9.5.3 關 閉FileInputStream

  當 你 完 成 一 個 文 件 的 操 作, 你 可 選 兩 種 方 法 關 閉 它: 顯式 關 閉 和 隱 式 關 閉, 隱 式 關 閉 是 自 動 垃 圾 回 收 時 的 功 能。

  顯 式 關 閉 如 下: myFileStream.close();

  9.6 例 程: 顯 示 一 個 文 件

  如 果 文 件 的 訪 問 權 限 足 夠, 你 可 以 在TextArea對 象 裏 顯 示文 件 內 容。

  下 面 是 顯 示 文 件 的 程 序 片 斷:

  FileInputStream fis; TextArea ta; public vod init(){ byte b[] = new byte [1024]; int I; //make it big enough or wait until you //know the size of the file String s; try { fis = new FileInputStream("/etc/motd"); } catch(FileNotFoundException e) { /*do something appropriate */ } try { I= fis.read(b); } catch(IOException e) { /* do something appropriate */ } s = new String(b, 0); ta = new TextArea(s,5,40); add (ta); }

  9.7 DataInputStreams

  DataInputStreams與FileInputStreams差 不 多。Data流 可 以 直 接 讀 任意 一 種 變 量 類 型, 如浮 點 數, 整 數 和 字 符 等。 一 般 來 說, 對二 進 制 文 件 使 用DataInputStream流。

  9.7.1 打 開 和 關 閉DataInputStreams

  打 開 和 關 閉DataInputStreams對 象 時, 其 方 法 與FileInputStreams相同:

  DataInputStreams myDataStream; FileInputStreams myFileStream;

  //get a file handle myFileStream = new FileInputStream("/usr/db/stock.dbf"); //open,or "chain" a data input file myDataStream = new DataOutputStream(myFileStream);

  //Now we can use both input streams to access our file //j(If we want to...) myFileStream.read(b); I = myDataStrea.readInt();

  //close the data friel explicityly //Always close the "topmost" file stream myDataStream.close(); myFileStream.close();

  9.7.2 讀DataInputStreams

  當 你 從DataInputStreams流 裏 訪 問 文 件 時, 你 可 以 使 用 與FileInputStream流相 同 的 成 員 函 數 read()。 但 你 也 可 以 使 用 其 他 訪 問 方 法 來讀 取 不 同 種 類 的 數 據:

  l byte readByte() l int readUnsignedByte() l short readShort() l int readUnsighedShort() l char readChar() l int readInt l long readLong() l float readFloat() l double readDouble() l String readLine()

  以 上 每 一 個 成 員 函 數 都 讀 取 相 應 的 數 據 對 象。 象String readLine()成 員 函 數, 你 可 使 用/n,/r,/r/n,或EOF作 爲 字 符 ?reg; 結束 符。

  讀 一 個 長 整 型, 例 如:

  long serialNo; ... serialNo = myDataStream.readLong();

  9.8 URL 輸 入 流

  除 了 基 本 文 件 訪 問 外,Java還 提 ?copy; 了 通 過 網 絡 使 用URL訪問 對 象 的 功 能。 在 下 面 這 個 例 子 裏, 我 們 用getDocumentBase()成員 函 數 並 顯 式 指 定URL對 象 來 訪 問 聲 音 和 圖 象。

  String imageFile = new String ("images/Duke/T1.gif"); images[0] = getImage(getDocumentBase(),imageFile();

  如 果 我 們 願 意, 可 以 直 接 使 用URL: URL imageSource; imageSource = new URL("http://555-1212.com/~info"); images[0] = getImage(imageSource,"Duke/T1.gif");

  我 們 可 以 爲 相 應 的URL打 開 輸 入 流。 例 如, 下 面 的 程 序 裏包 括 一 個 數 據 文 件: InputStream is; byte buffer[] = new byte[24]; is = new URL(getDocumentBase(),dataname).openStream();

  現 在 我 們 可 以 使 用is, 就 象 使 用FileInputStream對 象 一 樣: is.read(buffer.0,buffer.length);

  注 意: 有 ?copy; 用 戶 設 置 了 他 們 的 瀏 覽 器 安 全 屬 性, 可以 不 讓 你 的 程 序 訪 問 他 們 的 文 件。

  9.9 OutputStreams

  上 面 我 們 談 到 了 讀 數 據, 那 麼 如 何 實 現 寫 數 據 呢? 象 輸入 流 一 樣, 輸 出 流 也 有 類 似 的 層 次 結 構:

  OutputStream

  FileOutputStream PipedOutputStream ByteArrayOutputStream FilterOutputStream

  DataOutputStream PrintStream BufferedOutputStream

  我 們 將 分 析FileOutputStream和DataOutputStream類 來 完 成 我 們 碰到 的 輸 出 流 問 題。 其 它 的 輸 出 流 包 含 了 更 多 的 信 息 和 成員 函 數。 象 輸 入 流 的 源 文 件 一 樣, 這 ?copy; 文 件 在 $JAVA_HOME/src/java/io目錄 下。

  9.9.1 FileOutputStream類

  FileOutputStream對 象 用 於 向 一 個 文 本 文 件 寫 數 據。 象 輸 入文 件 一 樣, 你 得 先 打 開 這 個 文 件 後 才 能 寫 這 個 文 件。

  9.9.2 打 開 一 個FileOutputStream對 象

  要 打 開 一 個FileOutputStream對 象, 象 打 開 一 個 輸 入 流 一 樣,你 可 以 將 字 符 ?reg; 或 文 件 對 象 作 爲 參 數: FileOutputStream myFileStream; myFileStream = new FileOutputStream("/etc/motd");

  象 輸 入 流 一 樣, 你 也 可 這 樣 使 用: File myFile; FileOutputStream myFileStream; myFile = new File("/etc/motd"); myFileStream = new FileOutputStream(myFile);

  9.9.3 寫 入 一 個 流

  一 ?copy; 文 件 被 打 開, 你 便 可 以 使 用write()函 數 向 文 件 裏寫 一 ?copy; 數 據。 就 象 輸 入 流 的read()函 數 一 樣, 你 可 有 三種 方 法: l void write(int b);//writes out one byte l void write(byte b[]);//writes out entire array l void write (byte b[],int offset,int length);//write out length bytes of b[],starting at b[offset]

  9.9.4 關 閉 一 個FileOutputStream對 象

  關 閉 輸 出 流 和 關 閉 輸 入 流 方 法 一 樣, 你 可 以 使 用 顯 式方 法: myFileStream.close(); 你 也 可 以 讓 系 統 自 動 關 閉 它。

  9.10 例 子: 存 儲 信 息

  下 面 有 一 個 程 序, 讓 用 戶 輸 入 一 ?copy; 姓 名 和 電 話 號 碼。每 一 個 姓 名 和 號 碼 將 加 在 文 件 裏。 用 戶 通 過 點“Done"按鈕 來 告 訴 系 統 整 個 列 表 已 輸 入 完 畢。

  一 ?copy; 用 戶 輸 入 完 整 個 列 表, 程 序 將 創 建 一 個 輸 出 文件 並 顯 示 或 打 印 出 來。 例 如:

  555-1212,Tom 123-456-7890,Peggy L. 234-5678,Marc 234-5678,Ron 876-4321,Beth&Brian 33.1.42.45.70,Jean-Marc

  下 面 是 程 序 的 源 代 碼: import java.io.*;

  //Phones.java //A simple database creation program

  class Phones { static FileOutputStream fos; public static final int lineLength = 81; public static void main(String args[]) throws IOExciption { byte[] phone = new byte[lineLength]; byte[] name = new byte[lineLenght]; int I; fos = new FileOutputStream("phone.numbers"); while (true) { System.err.println("Enter a name (enter ’done’ to quit)"); readLine(name); if ("done".equalsIgnoreCase(new String(name,0,0,4))) { break; } System.err.println("Enter the phone number"); readLine(phone); for ( i=0;phone[i]!= 0;i++) { fos.write(phone[i]); } fos.write(’,’); for (i=0;name[i]!= 0;I++) { fos.write(name[i]); } fos.write(’/n’); } fos.close(); }

  private static void readLine(byte line[]) throws IOException { int i=0,b=0;

  while ((i
  9.11 BufferedOutput流

  如 果 你 處 理 的 數 據 量 很 多, 或 向 文 件 寫 很 多 次 小 數 據,你 可 以 使 用 一 個BufferedOutput流。 BufferedOutput流 提 ?copy; 和FileOutputStream類同 樣 的 寫 操 作 方 法, 但 所 有 輸 出 全 部 存 放 在 一 個 緩 衝 區裏。 當 你 填 滿 緩 衝 區, 它 將 一 次 性 寫 入 磁 盤。 或 者 你 主 動將 緩 衝 區 寫 入 磁 盤。

  9.11.1 創 建BufferedOutput流

  如 果 要 創 建 一 個BufferedOutput流, 首 先 需 要 一 個FileOutput流。然 後 將 緩 衝 區 鏈 接 到 FileOutput流: FileOutputStream myFileStream; BufferedOutputStream myBufferStream; //get a file handle myFileStream = new FileOutputStream("/usr/db/stock.dbf"); //chain a buffered output stream myBufferSSstream = new BufferedOutputStream(myFileStream);

  9.11.2 更 新 和 關 閉BufferedOutput流

  和 普 通FileOutput流 一 樣, 向BufferedOutput流 裏 的 每 一 次 寫 操作 和 寫 入 磁 盤 操 作 並 不 是 一 一 對 應 的。 要 想 在 程 序 結 束 ?reg; 前 將 緩 衝 區 裏 的 數 據 寫 入 磁 盤, 除 非 填 滿 緩 衝 區,否 則 只 有 顯 式 調 用flush()函 數: //force left-over data to disk myBufferStream.flush(); //close the data file explicitly //Always close the "topmost" file stream myBufferStream.close(); myFileStream.close();

  9.12 DataOutput流

  和DataInputStream對 應,Java還 提 ?copy; 了DataOutput流。 使 用DataOutput流,我 們 可 以 向 文 件 寫 入 二 進 制 數 據。

  9.12.1 打 開 和 關 閉DataOutput流 對 象

  打 開 和 關 閉DataOutput流 對 象 與 打 開、 關 閉FileOutput流 對 象方 法 一 樣: DataOutputStream myDataStream; FileOutputStream myFileStream; BufferedOutputStream myBufferStream;

  //get a file handle mhyFileStream = new FileOutputStream("/usr/db/stock.dbf"); //chain a buffered output stream (for efficiency); myBufferStream = new BufferedOutputStream(myFileStream); //chain a data output file myDataStream = new DataOutputStream(myBufferStream);

  //Now we can use both input streams to access our file //(iiIf we want to ...) myBufferStream.write(b); myDataStream.writeInt(i);

  //close the data file explicitly //Always colse the "topmost" file stream myDataStream.close(); myBuffersStream.close(); myFileStream.close();

  9.12.2 向DataOutput流 寫 數 據

  FileOutput流 裏 的write()函 數 各 種 方 法 都 適 用 於DataOutput流。你 還 可 以 看 到DataInput流 的 類 似 函 數 方 法: l void writeBoolean (boolean v) l void writeByte (int v) l void writeShort (int v) l void writeChar (int v) l void writeInt (int v) l void writeFloat (float v) l void writeDouble (double v) l void writeBytes (string s) l void writeChars (string s)

  對 字 符 ?reg; 來 說, 有 兩 種 選 擇:byte和char。 記 住byte是8位數 據 而char是16位 數 據。 如 果 你 想 利 用Unicode字 符 的 優 點, 你應 使 用writeChars()函 數。

  9.12.3 輸 出 記 數

  在 使 用 二 進 制 數 據 輸 出 時 常 用 的 另 外 一 個 函 數 是size()。這 個 函 數 返 回 寫 入 文 件 數 據 的 總 字 節 數。 你 也 可 用size()函數 將 數 據 文 件 分 成 四 字 節 爲 單 位 的 椋?例 如: ... int bytesLeft = myDataStream.size()%4; for (int I = 0; I< bytesLeft; I++) { myDataStrea.write(0); } ...

  9.13 隨 機 訪 問 文 件

  我 們 讀 文 件 常 常 不 是 從 頭 至 尾 順 序 讀 的。 你 也 許 想 將一 文 本 文 件 當 作 一 個 數 據 庫, 讀 完 一 個 記 錄 後, 跳 到 另 一個 記 錄, 它 們 在 文 件 的 不 同 地 方。Java提 ?copy; 了RandomAccessFile類讓 你 操 作 這 種 類 型 的 輸 入 輸 出。

  9.13.1 創 建 隨 機 訪 問 文 件

  打 開 隨 機 訪 問 文 件 有 兩 種 方 法: l 用 文 件 名 myRAFile = new RandomAccessFile(String name,String mode); l 用 文 件 對 象 myRAFile = new RandomAccessFile(File file,String mode);

  mode參 數 決 定 了 訪 問 文 件 的 權 限, 如 只 讀’r’或 讀 寫’wr’等。

  例 如, 我 們 打 開 一 個 數 據 庫 更 新 數 據: RandomAccessFile myRAFile; myRAFile = new RandomAccessFile("/usr/db/stock.dbf","rw");

  9.13.2 訪 問 信 息

  RandomAccessFile對 象 的 讀 寫 操 作 和DataInput/DataOutput對 象 的操 作 方 式 一 樣。 你 可 以 使 用 在DataInputStream 和DataOutputStream裏出 現 的 所 有read()和write()函 數。

  還 有 幾 個 函 數 幫 助 你 在 文 件 裏 移 動 指 針: l long getFilePointer(); 返 回 當 前 指 針 l void seek(long pos); 將 文 件 指 針 定 位 到 一 個絕 對 地 址。 地 址 是 相 對 於 文 件 頭 的 偏 移 量。 地 址0表 示 文 件的 開 頭。 l long length(); 返 回 文 件 的 長 度。 地 址"length()"表示 文 件 的 結 尾。

  9.13.3 增 加 信 息

  你 可 以 使 用 隨 機 訪 問 文 件 來 設 置 成 增 加 信 息 模 式: myRAFile = new RandomAccessFile("/tmp/java.log","rw"); myRAFile.seek(myRAFile.length()); //Any subsequent write()s will be appended to the file

  9.13.4 追 加 信 息 例 子 下 面 是 一 個 在 已 存 在 文 件 後 面 追 加字 符 ?reg; 的 例 子: import java.io.IOException; import java.io.RandomAccessFile;

  class raTest { public static void main(String args[]) throws IOException { RandomAccessFile myFAFile; String s = "Information to Append/nHi mom!/n"; //open our random access file myRAFile = new RandomAccessFile("/tmp/java.log","rw"); //move to the end of the file myRAFile.seek(myRAFile.length()); //Start appending! myRAFile.writeBytes(s);

  myRAFile.close(); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章