?? 9.html
字號:
<HTML><HEAD><TITLE>黃金書屋</TITLE><META content="text/html; charset=gb2312" http-equiv=Content-Type><link rel="stylesheet" href="/cssset.css"></HEAD><BODY bgColor=#ffffff leftMargin=0 link=#000000 topMargin=0 vLink=#000000><center> <script language="JavaScript" src="/top.js"></script></center><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%"> <center> <iframe width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no bordercolor="#000000" src="/banner3.html"></iframe> </center> <br> </TD> <TD bgColor=#c6bba4 vAlign=top width="12%"></TD> </TR></TABLE><TABLE background=images/bj.jpg border=0 cellSpacing=0 width="100%"> <TR> <TD bgColor=#c6bba4 vAlign=top width="11%" align="right"> <br><br><br> <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_end--></b> </TD> <TD bgColor=#c6bba4 vAlign=top width="1%"></TD> <TD background=/images/bj.jpg vAlign=top width="76%"> <br> <hr align="center" width="98%" size="1"> <br> <font color="blue">Java語言入門</font> >> <font color=red>第 九 章 Java輸 入 輸 出 操 作</font><hr width=90% color=#7A7A71><table width=88% border=0 align=center><tr><td><div style=font-size:12pt><pre> Java語言入門
第 九 章 Java輸 入 輸 出 操 作
9.1 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<args.length;i++){ fileToCheck = new File(args[i]);
info(fileToCheck); } } else{ System.out.println("No file given."); } }
public void info (File f) throws IOException { System.out.println("Name:
"+f.getName()); System.out.println("Path: "=f.getPath()); if
(f.exists()) { System.out.println("File exists.");
System.out.print((f.canRead() ?" and is Readable":""));
System.out.print((f.cnaWrite()?" and is Writeable":""));
System.out.println("."); System.out.println("File is " + f.lenght() = "
bytes."); } else { System.out.println("File does not exist."); } } }
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] =
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -