?? 9.html
字號:
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<lineLengh-1))&&((b=System.ini.read())!='\n')) { line[i++] =
(byte)b; } line[i]=(byte) 0; } }
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(); } }
本 章 小 結
1. Java通 過 系 統 類 達 到 訪 問 標 準 輸 入 輸 出 的 功 能。 2. 你
可 以 創 建、 讀、 寫 文 件。
</pre></div></td></tr></table><hr width=90% color=#7A7A71><!--end_content--> <br> </TD> <TD bgColor=#c6bba4 vAlign=top width="1%"></TD> <TD bgColor=#c6bba4 vAlign=bottom width="11%"><b><a href="/index.html">回首頁</a><br><br><a href='8.html'>上一頁</a><br><br><a href='http://pub.goldnets.com:81/bbs/post.php?job=new&topic=12210' target=_blank> 我有話說 </a><br><br><a href='http://pub.goldnets.com:81/bbs/bbslist.php?topic=12210' target=_blank> 評論一覽 </a></center><br><!--title_end1--></b><br></TD> </TR></TABLE><TABLE background=images/bj.jpg border=0 cellSpacing=0 width="100%"> <TR> <TD bgColor=#c6bba4 vAlign=top width="12%"> </TD> <TD background=/images/bj.jpg vAlign=top width="76%"> <br> <center> <iframe width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no bordercolor="#000000" src="/banner4.html"></iframe> </center> <center> <script language="JavaScript" src="http://www.myrice.com/bottom.js"></script> </center> </TD> <TD bgColor=#c6bba4 vAlign=top width="12%" align="left"></TD> </TR></TABLE><center> <script language="JavaScript" src="/bottom.js"></script></center></BODY></HTML>
<html><script language="JavaScript"> </script></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -