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

📄 0062.htm

📁 学习java必备书籍
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>第九章 Java的输入输出操作</strong></big></p>

<div align="right">---(文/袁小春)</div>

<h4 align=center>9.1 Java 输 入 输 出 流 </h4>

<p>所 有 的 程 序 语 言 都 提 ?copy; 与 本 机 文 件 系 统 交 互 的 方
式;Java也 不 例 外。 我 们 将 看 看Java是 怎 样 处 理 标 准 文 件 输 入
输 出 的(包 括stdin,stout,stderr)。 当 你 在 网 络 上 开 发 小 程 序 时,
你 必 须 注 意 直 接 文 件 输 入 输 出 是 不 安 全 因 素 的 关 键。 大
多 数 用 户 设 置 他 们 的 浏 览 器, 可 让 你 自 由 的 访 问 他 们 的
文 件 系 统, 但 有 ?copy; 不 让 你 访 问。 当 然, 如 果 你 开 发 你
内 部 的 应 用 程 序, 你 也 许 需 要 直 接 访 问 文 件。 </p>

<p>标 准 输 入 输 出 Unix的 用 户, 或 其 他 基 于 命 令 行 系 统 的 用
户(如DOS), 都 知 道 标 准 输 入 输 出 的 含 义。 标 准 输 入 文 件 是
键 盘, 标 准 输 出 文 件 是 你 的 终 端 屏 幕。 标 准 错 误 输 出 文 件
也 指 向 屏 幕, 如 果 有 必 要, 它 也 可 以 指 向 另 一 个 文 件 以 便
和 正 常 输 出 区 分。 </p>

<p>系 统 类 Java通 过 系 统 类 达 到 访 问 标 准 输 入 输 出 的 功 能。
上 面 提 到 的 三 个 文 件 在 这 个 系 统 类 中 实 现: 	Stdin 		System.in作
为InputStream类 的 一 个 实 例 来 实 现stdin, 你 可 以 使 用read()和skip(long
n)两 个 成 员 函 数。read()让 你 从 输 入 中 读 一 个 字 节,skip(long
n)让 你 在 输 入 中 跳 过n个 字 节。 </p>

<p>	Stout 		System.out作 为PrintStream来 实 现stdout, 你 可 以 使 用print()和println()两
个 成 员 函 数。 这 两 个 函 数 支 持Java的 任 意 基 本 类 型 作 为 参
数。 </p>

<p>	Stderr 		System.err同stdout一 样 实 现stderr。 象System.out一 样,
你 可 以 访 问PrintStream 成 员 函 数。 </p>

<h4 align=center>9.2 标 准 输 入 输 出 例 子 </h4>

<p>	 这 里 有 一 个 例 子, 功 能 象Unix里 的cat或type: </p>

<p>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(&quot;counted&quot;+count+&quot;total
bytes.&quot;); 	} } </p>

<h4 align=center>9.3 普 通 输 入 输 出 类	</h4>

<p>除 了 基 本 的 键 盘 输 入 和 屏 幕 输 出 外, 我 们 还 需 要 联 系
文 件 的 输 入 输 出。 我 们 将 学 习 下 面 几 个 类: l FileInputStream
l DataInputStream l FileOutputStream l DataOutputStream </p>

<p>作 为 参 考, 再 列 出 一 ?copy; 特 定 应 用 的 类: l PipedInputStream
l BufferedInputStream l PushBackInputStream l StreamTokenizer l PipedOutputStream
l BufferedOutputStream l RandomAccessFile </p>

<p>我 们 不 在 此 讨 论 这 ?copy; 类, 但 你 可 以 在JAVA_HOME/src/java/io目
录 里 查 看 每 个 类 的 成 员 函 数 定 义。 </p>

<h4 align=center>9.4 文 件 </h4>

<p>在 我 们 进 行 文 件 操 作 时, 需 要 知 道 一 ?copy; 关 于 文 件 的
信 息。File类 提 ?copy; 了 一 ?copy; 成 员 函 数 来 操 纵 文 件 和 获
得 一 ?copy; 文 件 的 信 息。 </p>

<h4 align=center>9.4.1 创 建 一 个 新 的 文 件 对 象 </h4>

<p>你 可 用 下 面 三 个 方 法 来 创 建 一 个 新 文 件 对 象: </p>

<p>	File 	myFile; 	myFile	= 	new File(&quot;etc/motd&quot;); </p>

<p>或 </p>

<p>	myFile = new File(&quot;/etc&quot;,&quot;motd&quot;); 	//more useful
if the directory or filename are variables </p>

<p>或 </p>

<p>	File myDir = new file(&quot;/etc&quot;); 	myFile = new File(myDir,&quot;motd&quot;);
</p>

<p>这 三 种 方 法 取 决 于 你 访 问 文 件 的 方 式。 例 如, 如 果 你 在
应 用 程 序 里 只 用 一 个 文 件, 第 一 种 创 建 文 件 的 结 构 是 最
容 易 的。 但 如 果 你 在 同 一 目 录 里 打 开 数 个 文 件, 则 第 二 种
或 第 三 种 结 构 更 好 一 ?copy;。 </p>

<h4 align=center>9.4.2 文 件 测 试 和 使 用 </h4>

<p>一 ?copy; 你 创 建 了 一 个 文 件 对 象, 你 便 可 以 使 用 以 下 成
员 函 数 来 获 得 文 件 相 关 信 息: </p>

<p>文 件 名 l String getName() l String getPath() l String getAbslutePath()
l String getParent() l boolean renameTo(File newName) </p>

<p>文 件 测 试	 l boolean exists() l boolean canWrite() l boolean canRead()
l boolean isFile() l boolean isDirectory() l boolean isAbsolute() </p>

<p>一 般 文 件 信 息 l long lastModified() l long length() </p>

<p>目 录 用 法 l boolean mkdir() l String[] list() </p>

<h4 align=center>9.4.3 文 件 信 息 获 取 例 子 程 序</h4>

<p>这 里 是 一 个 独 立 的 显 示 文 件 的 基 本 信 息 的 程 序, 文 件
通 过 命 令 行 参 数 传 输: </p>

<p>import java.io.*; class fileInfo{ 	File fileToCheck; 	public static
void main(String args[]) throws IOException{ 		if (args.length&gt;0){ 			for
(int i=0;i&lt;args.length;i++){ 				fileToCheck = new File(args[i]); 				info(fileToCheck);
			} 		} 		else{ 			System.out.println(&quot;No file given.&quot;); 		}
	} 	 	public void info (File f) throws IOException { 		System.out.println(&quot;Name:
&quot;+f.getName()); 		System.out.println(&quot;Path: &quot;=f.getPath());
		if (f.exists()) { 					System.out.println(&quot;File exists.&quot;);
					System.out.print((f.canRead() ?&quot; and is Readable&quot;:&quot;&quot;));
					System.out.print((f.cnaWrite()?&quot; and is Writeable&quot;:&quot;&quot;));
					System.out.println(&quot;.&quot;); 					System.out.println(&quot;File
is &quot; + f.lenght() = &quot; bytes.&quot;); 			} 		else { 			System.out.println(&quot;File
does not exist.&quot;); 		} 	} } </p>

<h4 align=center>9.5 输 入 流</h4>

<p>InputStream SequenceInputStream							FileInputStream 	PipedInputStream					ByteArrayInputStream
				FileterInputStream		StringBufferInputStream </p>

<p>DataInputStream						LineNumberInputStream 		PushbackInputStream		BufferedInputStream
有 好 几 个 类 是 专 门 用 来 处 理 文 件 输 入 的。 下 面 是 文 件 输
入 类 的 层 次 结 构: </p>

<h4 align=center>9.5.1 FileInputStream 对 象 </h4>

<p>FileInputStream典 型 地 表 示 一 种 顺 序 访 问 的 文 本 文 件。 通
过 使 用FileInputStream你 可 以 访 问 文 件 的 一 个 字 节、 几 个 字 节
或 整 个 文 件。 </p>

<h4 align=center>9.5.2 打 开FileInputStream </h4>

<p>为 一 个 文 件 打 开 输 入 流FileInputStream, 你 必 须 将 文 件 名
或 文 件 对 象 传 送 给 结 构: </p>

<p>FileInput Stream myFileStream; myFileStream = new FileInputStream (
&quot;/etc/motd&quot;); </p>

<p>你 还 可 以 象 下 边 这 样 从FileInputStream里 读 文 件 信 息: </p>

<p>File myFile ; FileInputSteam myFileStream; myFile = new File(&quot;/etc/motd&quot;);
myFileStream = new FileInputStream(myFile); </p>

<p>一 ?copy;FileInputStream输 入 流 打 开, 你 就 可 以 从 里 面 读 取
信 息 了。read()成 员 函 数 有 以 下 几 种 选 项: </p>

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

<h4 align=center>9.5.3 关 闭FileInputStream </h4>

<p>当 你 完 成 一 个 文 件 的 操 作, 你 可 选 两 种 方 法 关 闭 它: 显
式 关 闭 和 隐 式 关 闭, 隐 式 关 闭 是 自 动 垃 圾 回 收 时 的 功 能。
</p>

<p>显 式 关 闭 如 下: myFileStream.close(); </p>

<h4 align=center>9.6 例 程: 显 示 一 个 文 件 </h4>

<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(&quot;/etc/motd&quot;);
		} 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>

<h4 align=center>9.7 DataInputStreams</h4>

<p>DataInputStreams与FileInputStreams差 不 多。Data流 可 以 直 接 读 任
意 一 种 变 量 类 型, 如浮 点 数, 整 数 和 字 符 等。 一 般 来 说, 对
二 进 制 文 件 使 用DataInputStream流。 </p>

<h4 align=center>9.7.1 打 开 和 关 闭DataInputStreams</h4>

<p>打 开 和 关 闭DataInputStreams对 象 时, 其 方 法 与FileInputStreams相
同: </p>

<p>DataInputStreams myDataStream; FileInputStreams myFileStream; </p>

<p>//get a file handle myFileStream = new FileInputStream(&quot;/usr/db/stock.dbf&quot;);
//open,or &quot;chain&quot; 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 &quot;topmost&quot;
file stream myDataStream.close(); myFileStream.close(); </p>

<h4 align=center>9.7.2 读DataInputStreams </h4>

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

⌨️ 快捷键说明

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