📄 yyrm9.html
字号:
<P>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 </P>
<P>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. </P>
<H3 align=center>9.5.3 关 闭FileInputStream </H3>
<P>当 你 完 成 一 个 文 件 的 操 作, 你 可 选 两 种 方 法 关 闭 它: 显式 关 闭 和 隐 式 关 闭, 隐 式
关 闭 是 自 动 垃 圾 回 收 时 的 功 能。 </P>
<P>显 式 关 闭 如 下: myFileStream.close(); </P>
<H2 align=center>9.6 例 程: 显 示 一 个 文 件 </H2>
<P>如 果 文 件 的 访 问 权 限 足 够, 你 可 以 在TextArea对 象 里 显 示文 件 内 容。 </P>
<P>下 面 是 显 示 文 件 的 程 序 片 断: </P>
<P>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); } </P>
<H2 align=center>9.7 DataInputStreams</H2>
<P>DataInputStreams与FileInputStreams差 不 多。Data流 可 以 直 接 读 任意 一 种 变 量
类 型, 如浮 点 数, 整 数 和 字 符 等。 一 般 来 说, 对二 进 制 文 件 使 用DataInputStream流。
</P>
<H3 align=center>9.7.1 打 开 和 关 闭DataInputStreams</H3>
<P>打 开 和 关 闭DataInputStreams对 象 时, 其 方 法 与FileInputStreams相同: </P>
<P>DataInputStreams myDataStream; FileInputStreams myFileStream;
</P>
<P>//get a file handle myFileStream = new
FileInputStream("/usr/db/stock.dbf"); //open,or "chain" a data input
file myDataStream = new DataOutputStream(myFileStream); </P>
<P>//Now we can use both input streams to access our file //j(If we
want to...) myFileStream.read(b); I = myDataStrea.readInt(); </P>
<P>//close the data friel explicityly //Always close the "topmost"
file stream myDataStream.close(); myFileStream.close(); </P>
<H3 align=center>9.7.2 读DataInputStreams </H3>
<P>当 你 从DataInputStreams流 里 访 问 文 件 时, 你 可 以 使 用 与FileInputStream流相
同 的 成 员 函 数 read()。 但 你 也 可 以 使 用 其 他 访 问 方 法 来读 取 不 同 种 类 的 数 据:
</P>
<P>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() </P>
<P>以 上 每 一 个 成 员 函 数 都 读 取 相 应 的 数 据 对 象。 象String readLine()成 员 函 数,
你 可 使 用\n,\r,\r\n,或EOF作 为 字 符 ?reg; 结束 符。 </P>
<P>读 一 个 长 整 型, 例 如: </P>
<P>long serialNo; ... serialNo = myDataStream.readLong(); </P>
<H2 align=center>9.8 URL 输 入 流 </H2>
<P>除 了 基 本 文 件 访 问 外,Java还 提 ?copy; 了 通 过 网 络 使 用URL访问 对 象 的 功 能。 在
下 面 这 个 例 子 里, 我 们 用getDocumentBase()成员 函 数 并 显 式 指 定URL对 象 来 访 问 声
音 和 图 象。 </P>
<P>String imageFile = new String ("images/Duke/T1.gif"); images[0] =
getImage(getDocumentBase(),imageFile(); </P>
<P>如 果 我 们 愿 意, 可 以 直 接 使 用URL: URL imageSource; imageSource = new
URL("http://555-1212.com/~info"); images[0] =
getImage(imageSource,"Duke/T1.gif"); </P>
<P>我 们 可 以 为 相 应 的URL打 开 输 入 流。 例 如, 下 面 的 程 序 里包 括 一 个 数 据 文 件:
InputStream is; byte buffer[] = new byte[24]; is = new
URL(getDocumentBase(),dataname).openStream(); </P>
<P>现 在 我 们 可 以 使 用is, 就 象 使 用FileInputStream对 象 一 样:
is.read(buffer.0,buffer.length); </P>
<P>注 意: 有 ?copy; 用 户 设 置 了 他 们 的 浏 览 器 安 全 属 性, 可以 不 让 你 的 程 序 访 问 他
们 的 文 件。 </P>
<H2 align=center>9.9 OutputStreams </H2>
<P>上 面 我 们 谈 到 了 读 数 据, 那 么 如 何 实 现 写 数 据 呢? 象 输入 流 一 样, 输 出 流 也 有 类
似 的 层 次 结 构: </P>
<P>OutputStream </P>
<P>FileOutputStream PipedOutputStream ByteArrayOutputStream
FilterOutputStream </P>
<P>DataOutputStream PrintStream BufferedOutputStream </P>
<P>我 们 将 分 析FileOutputStream和DataOutputStream类 来 完 成 我 们 碰到 的 输 出 流
问 题。 其 它 的 输 出 流 包 含 了 更 多 的 信 息 和 成员 函 数。 象 输 入 流 的 源 文 件 一 样, 这
?copy; 文 件 在 $JAVA_HOME/src/java/io目录 下。 </P>
<H3 align=center>9.9.1 FileOutputStream类 </H3>
<P>FileOutputStream对 象 用 于 向 一 个 文 本 文 件 写 数 据。 象 输 入文 件 一 样, 你 得 先
打 开 这 个 文 件 后 才 能 写 这 个 文 件。 </P>
<H3 align=center>9.9.2 打 开 一 个FileOutputStream对 象 </H3>
<P>要 打 开 一 个FileOutputStream对 象, 象 打 开 一 个 输 入 流 一 样,你 可 以 将 字 符
?reg; 或 文 件 对 象 作 为 参 数: FileOutputStream myFileStream; myFileStream
= new FileOutputStream("/etc/motd"); </P>
<P>象 输 入 流 一 样, 你 也 可 这 样 使 用: File myFile; FileOutputStream
myFileStream; myFile = new File("/etc/motd"); myFileStream = new
FileOutputStream(myFile); </P>
<H3 align=center>9.9.3 写 入 一 个 流 </H3>
<P>一 ?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] </P>
<H3 align=center>9.9.4 关 闭 一 个FileOutputStream对 象 </H3>
<P>关 闭 输 出 流 和 关 闭 输 入 流 方 法 一 样, 你 可 以 使 用 显 式方 法:
myFileStream.close(); 你 也 可 以 让 系 统 自 动 关 闭 它。 </P>
<H2 align=center>9.10 例 子: 存 储 信 息 </H2>
<P>下 面 有 一 个 程 序, 让 用 户 输 入 一 ?copy; 姓 名 和 电 话 号 码。每 一 个 姓 名 和 号 码 将
加 在 文 件 里。 用 户 通 过 点“Done"按钮 来 告 诉 系 统 整 个 列 表 已 输 入 完 毕。 </P>
<P>一 ?copy; 用 户 输 入 完 整 个 列 表, ?序 将 创 建 一 个 输 出 文件 并 显 示 或 打 印 出 来。
例 如: </P>
<P>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 </P>
<P>下 面 是 程 序 的 源 代 码: import java.io.*; </P>
<P>//Phones.java //A simple database creation program </P>
<P>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(); } </P>
<P>private static void readLine(byte line[]) throws IOException {
int i=0,b=0; </P>
<P>while
((i<lineLengh-1))&&((b=System.ini.read())!='\n')) {
line[i++] = (byte)b; } line[i]=(byte) 0; } } </P>
<H2 align=center>9.11 BufferedOutput流 </H2>
<P>如 果 你 处 理 的 数 据 量 很 多, 或 向 文 件 写 很 多 次 小 数 据,你 可 以 使 用 一
个BufferedOutput流。 BufferedOutput流 提 ?copy; 和FileOutputStream类同 样 的 写
操 作 方 法, 但 所 有 输 出 全 部 存 放 在 一 个 缓 冲 区里。 当 你 填 满 缓 冲 区, 它 将 一 次 性 写
入 磁 盘。 或 者 你 主 动将 缓 冲 区 写 入 磁 盘。 </P>
<H3 align=center>9.11.1 创 建BufferedOutput流 </H3>
<P>如 果 要 创 建 一 个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); </P>
<H3 align=center>9.11.2 更 新 和 关 闭BufferedOutput流 </H3>
<P>和 普 通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(); </P>
<H2 align=center>9.12 DataOutput流 </H2>
<P>和DataInputStream对 应,Java还 提 ?copy; 了DataOutput流。 使 用DataOutput流,我
们 可 以 向 文 件 写 入 二 进 制 数 据。 </P>
<H3 align=center>9.12.1 打 开 和 关 闭DataOutput流 对 象 </H3>
<P>打 开 和 关 闭DataOutput流 对 象 与 打 开、 关 闭FileOutput流 对 象方 法 一 样:
DataOutputStream myDataStream; FileOutputStream myFileStream;
BufferedOutputStream myBufferStream; </P>
<P>//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); </P>
<P>//Now we can use both input streams to access our file //(iiIf we
want to ...) myBufferStream.write(b); myDataStream.writeInt(i); </P>
<P>//close the data file explicitly //Always colse the "topmost"
file stream myDataStream.close(); myBuffersStream.close();
myFileStream.close(); </P>
<H3 align=center>9.12.2 向DataOutput流 写 数 据</H3>
<P>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) </P>
<P>对 字 符 ?reg; 来 说, 有 两 种 选 择:byte和char。 记 住byte是8位数 据 而char是16位 数
据。 如 果 你 想 利 用Unicode字 符 的 优 点, 你应 使 用writeChars()函 数。 </P>
<H3 align=center>9.12.3 输 出 记 数 </H3>
<P>在 使 用 二 进 制 数 据 输 出 时 常 用 的 另 外 一 个 函 数 是size()。这 个 函 数 返 回 写 入 文
件 数 据 的 总 字 节 数。 你 也 可 用size()函数 将 数 据 文 件 分 成 四 字 节 为 单 位 的 块, 例 如:
... int bytesLeft = myDataStream.size()%4; for (int I = 0; I<
bytesLeft; I++) { myDataStrea.write(0); } ... </P>
<H2 align=center>9.13 随 机 访 问 文 件</H2>
<P>我 们 读 文 件 常 常 不 是 从 头 至 尾 顺 序 读 的。 你 也 许 想 将一 文 本 文 件 当 作 一 个 数 据
库, 读 完 一 个 记 录 后, 跳 到 另 一个 记 录, 它 们 在 文 件 的 不 同 地 方。Java提 ?copy;
了RandomAccessFile类让 你 操 作 这 种 类 型 的 输 入 输 出。 </P>
<H3 align=center>9.13.1 创 建 随 机 访 问 文 件 </H3>
<P>打 开 随 机 访 问 文 件 有 两 种 方 法: l 用 文 件 名 myRAFile = new
RandomAccessFile(String name,String mode); l 用 文 件 对 象 myRAFile =
new RandomAccessFile(File file,String mode); </P>
<P>mode参 数 决 定 了 访 问 文 件 的 权 限, 如 只 读'r'或 读 写'wr'等。 </P>
<P>例 如, 我 们 打 开 一 个 数 据 库 更 新 数 据: RandomAccessFile myRAFile;
myRAFile = new RandomAccessFile("/usr/db/stock.dbf","rw"); </P>
<H3 align=center>9.13.2 访 问 信 息</H3>
<P>RandomAccessFile对 象 的 读 写 操 作 和DataInput/DataOutput对 象 的操 作 方 式 一
样。 你 可 以 使 用 在DataInputStream 和DataOutputStream里出 现 的 所
有read()和write()函 数。 </P>
<P>还 有 几 个 函 数 帮 助 你 在 文 件 里 移 动 指 针: l long getFilePointer(); 返 回 当
前 指 针 l void seek(long pos); 将 文 件 指 针 定 位 到 一 个绝 对 地 址。 地 址 是 相 对 于
文 件 头 的 偏 移 量。 地 址0表 示 文 件的 开 头。 l long length(); 返 回 文 件 的 长 度。 地
址"length()"表示 文 件 的 结 尾。 </P>
<H3 align=center>9.13.3 增 加 信 息 </H3>
<P>你 可 以 使 用 随 机 访 问 文 件 来 设 置 成 增 加 信 息 模 式: myRAFile = new
RandomAccessFile("/tmp/java.log","rw");
myRAFile.seek(myRAFile.length()); //Any subsequent write()s will be
appended to the file </P>
<P>9.13.4 追 加 信 息 例 子 下 面 是 一 个 在 已 存 在 文 件 后 面 追 加字 符 ?reg; 的 例 子:
import java.io.IOException; import java.io.RandomAccessFile; </P>
<P>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); </P>
<P>myRAFile.close(); } } </P>
<H2 align=center>本 章 小 结 </H2>
<P>1. Java通 过 系 统 类 达 到 访 问 标 准 输 入 输 出 的 功 能。 2. 你可 以 创 建、 读、 写 文
件。 </P></TD>
<TR>
<TR>
<TD bgColor=#f0f0f0 borderColor=#ffffff class=t1 noWrap
width="50%"><IMG height=12 src="yyrm9.files/yellow_dot.gif"
width=12><A
href="http://www.chinajavaworld.net/doc/yyrm/yyrm8.html">上一章</A> </TD>
<TD bgColor=#f0f0f0 borderColor=#ffffff class=t1 noWrap
width="50%"><IMG height=12 src="yyrm9.files/blue_dot.gif"
width=12><A
href="http://www.chinajavaworld.net/doc/yyrm/yyrm1.html">第一章</A> </TD>
<TR></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--end first table --><!-- start foot -->
<SCRIPT src="yyrm9.files/foot1.js"></SCRIPT>
<SCRIPT src="yyrm9.files/adsindex.js"></SCRIPT>
<SCRIPT src="yyrm9.files/foot2.js"></SCRIPT>
<!-- end foot --></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -