⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 9.html

📁 这是介绍JAVE 语言的书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
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%">&nbsp;</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 + -