最近复习了一下IO流部分,觉得还是记录一下 不然又忘了 虽然网上有很多人写过了 还是自己写一下记忆深刻点吧
java IO流 可以按处理数据的单位分为:字节流和字符流 当然也可以按着流的角色分为:节点流(直接作用于文件)和处理流(对节点流的包装)
一些基本的IO流类和分类:
当然我还是比较关注那些常用的IO流:
抽象基类:InputStream,OutputStream,Reader,Writer.
继承基类的节点流类:FileInputStream,FileOutputStream,FIleReader,FileWriter
对节点流包装的处理流:BufferedInputStream ,BufferedOutputStream,BufferedReader,BufferedWriter
以节点流为例,写一个文件复制的函数:
// 节点流实现文件复制的方法
public void copyFile(String src, String dest) {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[1024];//存放每次read()的字节数据
int len;
while ((len = fis.read(b)) != -1) {//len表示输入流读入字节数组的长度,当read()返回-1是表示读到末尾
// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
缓冲流:也是文件复制的函数
前面说过 缓冲流是对节点流的包装,从代码中也可窥一斑,缓冲流较节点流在效率上有很大提高,开发中较常用!
//使用缓冲流实现文件的复制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中 可以视为对节点流的包装,个人理解
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();
}
}catch (IOException e) {
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相应的字符流所对应的节点流(FileReader,FileWriter)与缓冲流(BufferedReader,BufferWriter)特性基本与字节流相同:直接上代码吧
//字符节点流实现文本文件的复制操作
public void testFileReaderWriter(String src1,String dest1){
//1.输入流对应的文件src一定要存在,否则抛异常。输出流对应的文件dest可以不存在,执行过程中会自动创建
FileReader fr = null;
FileWriter fw = null;
try{
//不能实现非文本文件的复制
File src = new File(src1);
File dest = new File(dest1);
//2.创建字符节点流 字符流只能处理文本文件,处理二进制文件会出错
fr = new FileReader(src);
fw = new FileWriter(dest);
//3.读写操作
char[] c = new char[24];
int len;
while((len = fr.read(c)) != -1){
fw.write(c, 0, len);
}
}catch(Exception e){
e.printStackTrace();
}finally{//关闭流资源
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//使用字符缓冲流实现文件的复制的方法
public void copyFile(String src,String dest){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
//2.想创建相应的节点流:FileInputStream、FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中 视为对节点流的包装
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//4.具体的实现文件复制的操作
byte[] b = new byte[1024];
int len;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
bos.flush();//刷新缓存
}
}catch (IOException e) {
e.printStackTrace();
}finally{
//5.关闭相应的流
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}