OutputStream 是所有表示位输出流的类之父类,它是一个抽象类。子类要重新定义其中所定义的抽象方法,OutputStream是用于将数据写入目的地的抽象表示。例如 System中的标准输出流对象out其类型是java.io.PrintStream,这个类是OutputStream的子类 (java.io.FilterOutputStream继承OutputStream, PrintStream再继承FilterOutputStream)。在程序开始之后,out流对象就会开启,可以通过out来将数据写至目的地装置, 这个装置通常是屏幕显示或用户定义的输出装置。
一般来说,很少直接实现InputStream或OutputStream上的方法,因为这些方法比较低级,通常会实现它们的子类。这些子类上所定义的方法在进行输入/输出时更为方便。
FileInputStream和FileOutputStream 以及缓冲的BufferedInputStream BufferedOutputStream
FileInputStream
1、public int read()
从输入流读取下一个数据字节。返回 0 到 255 范围内的 int 字节值。如果因已到达流末尾而没有可用的字节,则返回值 -1。
2、public int read(byte[] b)
从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少可以读取一个字节并将其存储在 b 中。此方法等同于read(b, 0, b.length)
3、public int read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入字节数组。尝试读取多达 len 字节,但可能读取较少数量。以整数形式返回实际读取的字节数。如果由于已到达流末尾而不再有数据,则返回 -1。
参数:b - 读入数据的缓冲区。off - 在其处写入数据的数组 b 的初始偏移量。len - 要读取的最大字节数。
FileOutputStream
1、public void write(int b)
将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。
2、public void write(byte[] b)
将 b.length 个字节从指定的字节数组写入此输出流。write(b) 的常规协定是:应该与调用 write(b, 0, b.length) 的效果完全相同。
3、public void write(byte[] b, int off, int len)
将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。write(b, off, len) 的常规协定是:将数组 b 中的某些字节按顺序写入输出流;元素 b[off] 是此操作写入的第一个字节,b[off+len-1] 是此操作写入的最后一个字节。
参数:b - 数据。off - 数据中的初始偏移量。len - 要写入的字节数。
实例
当建立一个FileInputStream或FileOutputStream的实例时,必须指定文件位置及文件名称,实例被建立时文件的流就会开启;而不使用流时,必须关闭文件流,以释放与流相依的系统资源,完成文件读/写的动作。
FileInputStream可以使用 read()方法一次读入一个字节,并以int类型返回,或者是使用read()方法时读入至一个byte数组,byte数组的元素有多少个,就读入多少 个字节。在将整个文件读取完成或写入完毕的过程中,这么一个byte数组通常被当作缓冲区,因为这么一个byte数组通常扮演承接数据的中间角色。
最基本实例1-1
try {
InputStream fis = new FileInputStream("D:\\Workspaces\\WeiboTest\\saved\\err.txt");
long num = 0; //读取字节计数
int bt = 0; //每次读入字节内容
//当读入文件末尾时,读入数据的值为-1
//每次读入一个字节,存放到变量bt中,直到读完整个文件
while ((bt = fis.read()) != -1) {
//System.out.print(bt); //以数字的形式逐个输出文件的每个字节
System.out.print((char) bt); //以字母的形式逐个输出文件的每个字节
num++;
}
System.out.println("读取的字节数为" + num);
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
写入任意文件1-2
File file = new File("D:\\Workspaces\\WeiboTest\\saved"+File.separator+filename+".html");
OutputStream ots= new FileOutputStream(file);
byte b[]=strentity.getBytes();
for(int i=0;i<b.length;i++){
ots.write(b[i]);
}
ots.close();
缓冲实例2
用FileInputStream或FileOutputStream创建缓冲类
/**
* 缓冲的字节流测试
*/
public static void testBufferedStream() {
int buffer = 10; //缓冲大小
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\x.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\bf2.txt"));
int bench = 0;
byte bts[] = new byte[buffer]; //创建字节流缓存
while ((bis.read(bts)) != -1) {
bos.write(bts); //将字节写入输出流中,实现文件的copy功能
bench++;
}
System.out.println("bench=" + bench);
//将输入流缓冲区中的数据全部写出(千万记住)
bos.flush();
bis.close();
bos.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定的文件!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取时发生IO异常!");
e.printStackTrace();
}
}
实例3 读取1024个字节,余额不足1024则逐个读 // avilable()可取得未读取数据的长度
try {
byte[] buffer = new byte[1024];
// 源文件
FileInputStream fileInputStream = new FileInputStream(new File(
args[0]));
FileOutputStream fileOutputStream = new FileOutputStream(new File(
args[1]));
// avilable()可取得未读取数据的长度
System.out.println("复制文件:" + fileInputStream.available() + "字节");
while (true) {
if (fileInputStream.available() < 1024) {
// 剩于的数据比1024少
// 一位一位读出再写入目的文件
int remain = -1;
while ((remain = fileInputStream.read()) != -1) {
fileOutputStream.write(remain);
}
break;
} else {
// 从来源文件读取数据至缓冲区
fileInputStream.read(buffer);
// 将数组数据写入目的文件
fileOutputStream.write(buffer);
}
}
// 关闭流
fileInputStream.close();
fileOutputStream.close();
System.out.println("复制完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}