9.8 URL 输入流
除了基本文件访问外,Java还 提到了通过网络使用URL访问对象的功能。 在下面这个例子里,我们用getDocumentBase()成员函数并显式指定URL对象来访 问声音和图象。
String imageFile = new String ("images/Duke/T1.gif");
images[0] = 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);
注意:有的用户设置了他们的浏览器安全属性,可以不让你的程序访问他们的文件。
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对象,象打开一个输入流一样,你可以将字符或文件对象 作为参数:
FileOutputStream myFileStream = new FileOutputStream("/etc/motd");
象输入流一样,你也可这样使用:
File myFile; // 定义文件对象
FileOutputStream myFileStream; // 定义流对象
myFile = new File("/etc/motd"); // 连接文件对象
myFileStream = new FileOutputStream(myFile); // 将文件对象读入流
9.9.3 写入一个流
文件被打开,你便可以使用write()函数向文件里写数据。就象输入流的read()函数一样, 你可有三种方法:
void write(int b);//writes out one byte
void write(byte b[]);//writes out entire array
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 例子:存储信息
下面的程序让用户输入姓名和电话号码。每一个姓名和号码将加在文件里。用 户通过点“Done"按钮来告诉系统整个列表已输入完毕。
用户输入完整个列表,程序将创建一个输出文件并显示或打印出来。例如:
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
public 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 } line[i]=(byte) 0; } } 9.11 BufferedOutput流 如果你处理的数据量很多,或向文件写很多次小数据,你可以使用一个BufferedOutput流。 BufferedOutput流和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流里的每一次写操作和写入磁盘 操作并不是一一对应的。要想在程序结束;前将缓冲区里的数据写入磁盘,除非填满缓冲区,否则只有显式调用flush()函数: //force left-over data to disk myBufferStream.flush(); //close the data file explicitly //Always close the "topmost" file stream myBufferStream.close(); myFileStream.close();
① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;
② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。