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

📄 0062.htm

📁 学习java必备书籍
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>long serialNo; ... serialNo = myDataStream.readLong(); </p>

<h4 align=center>9.8 URL 输 入 流 </h4>

<p>除 了 基 本 文 件 访 问 外,Java还 提 ?copy; 了 通 过 网 络 使 用URL访
问 对 象 的 功 能。 在 下 面 这 个 例 子 里, 我 们 用getDocumentBase()成
员 函 数 并 显 式 指 定URL对 象 来 访 问 声 音 和 图 象。 </p>

<p>String imageFile = new String (&quot;images/Duke/T1.gif&quot;); images[0]
= getImage(getDocumentBase(),imageFile(); </p>

<p>如 果 我 们 愿 意, 可 以 直 接 使 用URL: URL imageSource; imageSource
= new URL(&quot;http://555-1212.com/~info&quot;); images[0] = getImage(imageSource,&quot;Duke/T1.gif&quot;);
</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>

<h4 align=center>9.9 OutputStreams </h4>

<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>

<h4 align=center>9.9.1 FileOutputStream类 </h4>

<p>FileOutputStream对 象 用 于 向 一 个 文 本 文 件 写 数 据。 象 输 入
文 件 一 样, 你 得 先 打 开 这 个 文 件 后 才 能 写 这 个 文 件。 </p>

<h4 align=center>9.9.2 打 开 一 个FileOutputStream对 象 </h4>

<p>要 打 开 一 个FileOutputStream对 象, 象 打 开 一 个 输 入 流 一 样,
你 可 以 将 字 符 ?reg; 或 文 件 对 象 作 为 参 数: FileOutputStream
myFileStream; myFileStream = new FileOutputStream(&quot;/etc/motd&quot;);
</p>

<p>象 输 入 流 一 样, 你 也 可 这 样 使 用: File myFile; FileOutputStream
myFileStream; myFile = new File(&quot;/etc/motd&quot;); myFileStream =
new FileOutputStream(myFile); </p>

<h4 align=center>9.9.3 写 入 一 个 流 </h4>

<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>

<h4 align=center>9.9.4 关 闭 一 个FileOutputStream对 象 </h4>

<p>关 闭 输 出 流 和 关 闭 输 入 流 方 法 一 样, 你 可 以 使 用 显 式
方 法: myFileStream.close(); 你 也 可 以 让 系 统 自 动 关 闭 它。 </p>

<h4 align=center>9.10 例 子: 存 储 信 息 </h4>

<p>下 面 有 一 个 程 序, 让 用 户 输 入 一 ?copy; 姓 名 和 电 话 号 码。
每 一 个 姓 名 和 号 码 将 加 在 文 件 里。 用 户 通 过 点“Done&quot;按
钮 来 告 诉 系 统 整 个 列 表 已 输 入 完 毕。 </p>

<p>一 ?copy; 用 户 输 入 完 整 个 列 表, 程 序 将 创 建 一 个 输 出 文
件 并 显 示 或 打 印 出 来。 例 如: </p>

<p>555-1212,Tom 123-456-7890,Peggy L. 234-5678,Marc 234-5678,Ron 876-4321,Beth&amp;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(&quot;phone.numbers&quot;); 		while
(true) { 			System.err.println(&quot;Enter a name (enter 'done' to quit)&quot;);
			readLine(name); 			if (&quot;done&quot;.equalsIgnoreCase(new String(name,0,0,4)))
{ 				break; 			} 			System.err.println(&quot;Enter the phone number&quot;);
			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&lt;lineLengh-1))&amp;&amp;((b=System.ini.read())!='\n'))
{ 		line[i++] = (byte)b; 	} 	line[i]=(byte) 0; 	} } </p>

<h4 align=center>9.11 BufferedOutput流 </h4>

<p>如 果 你 处 理 的 数 据 量 很 多, 或 向 文 件 写 很 多 次 小 数 据,
你 可 以 使 用 一 个BufferedOutput流。 BufferedOutput流 提 ?copy; 和FileOutputStream类
同 样 的 写 操 作 方 法, 但 所 有 输 出 全 部 存 放 在 一 个 缓 冲 区
里。 当 你 填 满 缓 冲 区, 它 将 一 次 性 写 入 磁 盘。 或 者 你 主 动
将 缓 冲 区 写 入 磁 盘。 </p>

<h4 align=center>9.11.1 创 建BufferedOutput流 </h4>

<p>如 果 要 创 建 一 个BufferedOutput流, 首 先 需 要 一 个FileOutput流。
然 后 将 缓 冲 区 链 接 到 FileOutput流: FileOutputStream myFileStream;
BufferedOutputStream myBufferStream; //get a file handle myFileStream =
new FileOutputStream(&quot;/usr/db/stock.dbf&quot;); //chain a buffered
output stream myBufferSSstream = new BufferedOutputStream(myFileStream);
</p>

<h4 align=center>9.11.2 更 新 和 关 闭BufferedOutput流 </h4>

<p>和 普 通FileOutput流 一 样, 向BufferedOutput流 里 的 每 一 次 写 操
作 和 写 入 磁 盘 操 作 并 不 是 一 一 对 应 的。 要 想 在 程 序 结 束
?reg; 前 将 缓 冲 区 里 的 数 据 写 入 磁 盘, 除 非 填 满 缓 冲 区,
否 则 只 有 显 式 调 用flush()函 数: //force left-over data to disk myBufferStream.flush();
//close the data file explicitly //Always close the &quot;topmost&quot;
file stream myBufferStream.close(); myFileStream.close(); </p>

<h4 align=center>9.12 DataOutput流 </h4>

<p>和DataInputStream对 应,Java还 提 ?copy; 了DataOutput流。 使 用DataOutput流,
我 们 可 以 向 文 件 写 入 二 进 制 数 据。 </p>

<h4 align=center>9.12.1 打 开 和 关 闭DataOutput流 对 象 </h4>

<p>打 开 和 关 闭DataOutput流 对 象 与 打 开、 关 闭FileOutput流 对 象
方 法 一 样: DataOutputStream myDataStream; FileOutputStream myFileStream;
BufferedOutputStream myBufferStream; </p>

<p>//get a file handle mhyFileStream = new FileOutputStream(&quot;/usr/db/stock.dbf&quot;);
//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 &quot;topmost&quot;
file stream myDataStream.close(); myBuffersStream.close(); myFileStream.close();
</p>

<h4 align=center>9.12.2 向DataOutput流 写 数 据</h4>

<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>

<h4 align=center>9.12.3 输 出 记 数 </h4>

<p>在 使 用 二 进 制 数 据 输 出 时 常 用 的 另 外 一 个 函 数 是size()。
这 个 函 数 返 回 写 入 文 件 数 据 的 总 字 节 数。 你 也 可 用size()函
数 将 数 据 文 件 分 成 四 字 节 为 单 位 的 块, 例 如: ... int bytesLeft
= myDataStream.size()%4; for (int I = 0; I&lt; bytesLeft; I++) { 		myDataStrea.write(0);
	} ... </p>

<h4 align=center>9.13 随 机 访 问 文 件</h4>

<p>我 们 读 文 件 常 常 不 是 从 头 至 尾 顺 序 读 的。 你 也 许 想 将
一 文 本 文 件 当 作 一 个 数 据 库, 读 完 一 个 记 录 后, 跳 到 另 一
个 记 录, 它 们 在 文 件 的 不 同 地 方。Java提 ?copy; 了RandomAccessFile类
让 你 操 作 这 种 类 型 的 输 入 输 出。 </p>

<h4 align=center>9.13.1 创 建 随 机 访 问 文 件 </h4>

<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(&quot;/usr/db/stock.dbf&quot;,&quot;rw&quot;);
</p>

<h4 align=center>9.13.2 访 问 信 息</h4>

<p>RandomAccessFile对 象 的 读 写 操 作 和DataInput/DataOutput对 象 的
操 作 方 式 一 样。 你 可 以 使 用 在DataInputStream 和DataOutputStream里
出 现 的 所 有read()和write()函 数。 </p>

<p>还 有 几 个 函 数 帮 助 你 在 文 件 里 移 动 指 针: l long getFilePointer();
返 回 当 前 指 针 l void seek(long pos); 将 文 件 指 针 定 位 到 一 个
绝 对 地 址。 地 址 是 相 对 于 文 件 头 的 偏 移 量。 地 址0表 示 文 件
的 开 头。 l long length(); 返 回 文 件 的 长 度。 地 址&quot;length()&quot;表
示 文 件 的 结 尾。 </p>

<h4 align=center>9.13.3 增 加 信 息 </h4>

<p>你 可 以 使 用 随 机 访 问 文 件 来 设 置 成 增 加 信 息 模 式: myRAFile
= new RandomAccessFile(&quot;/tmp/java.log&quot;,&quot;rw&quot;); 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 = &quot;Information to Append\nHi
mom!\n&quot;; 	 		//open our random access file 		myRAFile = new RandomAccessFile(&quot;/tmp/java.log&quot;,&quot;rw&quot;);
		//move to the end of the file 		myRAFile.seek(myRAFile.length()); 		//Start
appending! 		myRAFile.writeBytes(s); </p>

<p>		myRAFile.close();	 	} } </p>

<h4 align=center>本 章 小 结 </h4>

<p>1. Java通 过 系 统 类 达 到 访 问 标 准 输 入 输 出 的 功 能。 2. 你
可 以 创 建、 读、 写 文 件。 </p>

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -