文件输入/出字节流、文件缓冲输入/出字节流、文件输入/出字符流、文件缓冲输入/出字符流复制文件

来源:互联网 发布:淘宝有卖二手丝袜吗 编辑:程序博客网 时间:2024/06/06 15:54

简单记:

字节流:读取的单位是二进制;

字符流:读取的单位是字符(对二进制数据进行了编码和解码);

1.文件输入/出字节流 复制文件

InputStream是所有输入字节流的父类;OutputStream是所有输出字节流的父类。

FileInputStream是InputStream的子类,FileOutputStream是OutputStream的子类;

代码如下:

public static void copyPic(){

FileInputStream in = null;//输入流

FileOutputStream out = null;//输出流

File file = new File("G:/files/1.jpg");//需要拷贝的文件

try {

in = new FileInputStream(file);//建立输入流通道

out = new FileOutputStream(new File("G:/1.jpg"));//建立输出流通道

byte[] buffer = new byte[1024];//数据缓冲数组,因为读取到的数据是字节,所以使用字节数组来接数据

int len = 0;

//读写过程

while((len = in.read(buffer))!=-1){

out.write(buffer,0,len);//写入数据,每次读取多少数据就写入多少数据

}

} catch (IOException e) {

System.out.println("复制文件出现异常!!");

//抛出一个运行时异常,并且把产生的异常也抛出去,调用者也不用处理该异常

throw new RuntimeException(e);

}finally{

if(out != null){

try {

out.close();

System.out.println("关闭输出流成功!!");

} catch (IOException e) {

System.out.println("关闭输出流失败!!");

throw new RuntimeException(e);

}finally{

if(in != null){

try {

in.close();

System.out.println("关闭输入流成功!!");

} catch (IOException e) {

System.out.println("关闭输入流失败!!");

throw new RuntimeException(e);

}

}

}

}

 

}

System.out.println("复制文件成功!!");

}

注意:1.文件输出字节流FileOutputStream,调用构造方法 new FileOutputStream(File )的时候,是要先清空文件数据的再写入文件,如果想要追加文件内容的话,

需要使用newFileOutputStream(File ,true)构造函数;

2.在输入/出流使用结束之后,注意一定要关闭流,如果有多个流需要关闭遵循:先开后关,后开先关的原则;

2.文件缓冲输入/出字节流

BufferedInputStream是FileInputStream的子类,BufferedOutputStream是FileOutputStream的子类;

缓冲字节流产生的目的在于提高读写效率,内部维护了一个8kb的数组,每次读写的时候将数据读写进数组

(相当于一个缓冲区,也就是在内存操作的,而文件输入/出流是直接读取硬盘中的数据),当数组满了才输出,提高了效率。


复制文件的代码:

File file =new File("G:/files/1.jpg");

File fileOut = new File("G:/files/copy.jpg");

BufferedInputStream bufferedIn = null;

BufferedOutputStream bufferedOut = null;

try {

//建立文件输入通道

FileInputStream in = new FileInputStream(file);

//建立缓冲输入流

bufferedIn = new BufferedInputStream(in);//实质上缓冲输入字节流并不真正的读取数据,而是通过提供的FileInputStream 参数读取数据

//建立文件输出流通道

FileOutputStream out = new FileOutputStream(fileOut);

//建立缓冲输出流

bufferedOut = new BufferedOutputStream(out);//同缓冲输入字节流

//读取文件

byte[] buffer = new byte[1024];

int len = 0;

while((len = bufferedIn.read(buffer))!=-1){

bufferedOut.write(buffer, 0, len);

}

System.out.println("复制图片成功!");

} catch (IOException e) {

System.out.println("复制图片失败!");

throw new RuntimeException(e);

}finally{

//关闭流

try {

bufferedOut.close();

System.out.println("关闭输出流成功!");

} catch (IOException e) {

System.out.println("关闭输出流失败!");

throw new RuntimeException(e);

}finally{

try {

bufferedIn.close();

System.out.println("关闭输入流成功!");

} catch (IOException e) {

System.out.println("关闭输入流失败!");

throw new RuntimeException(e);

}

}

}

注意:1.使用文件缓冲字节流输出文件的时候,由于是要写入数组中的,所以数组没有满的时候是不会输出的,所以要调用out.flush()方法或者out.close()方法来

将数组中的数据刷到文件中;

2.此处使用缓冲字节流,也需要关闭流,由于缓冲字节流并不是真正的读取文件,所以它的close()方法是关闭传入的文件输入/出流文件的,所以不需要再次关闭

文件输入/出流。


3.文件输入/出字符流

Reader是所有字符流的基类;Writer是所有字符流的基类;

FileReader/FileWriter是读取文件的输入字符流;

File file = new File("D:/1.jpg");

File fileOut = new File("G:/files/2.jpg");

FileReader reader = new FileReader(file);

FileWriter writer = new FileWriter(fileOut);

char[] buffer = new char[1024];

int len = 0;

while((len =reader.read(buffer))!=-1){

writer.write(buffer,0,len);

}

reader.close();

writer.close();

注意:1.没有做异常处理。只抛出了异常;

2.使用文件输入、输出字符流读取图片文件的时候,副本文件大小和原文件的大小不一致,大小偏小,是因为使用字符流需要对二进制进行编码和解码,默认

使用GBK方式,当读取一个二进制数据的时候,很可能是一个在GBK码表中没有对应字符的一个数据,没有找到对应的字符则会找到GBK的一个未知字符

作为返回的字符,因此导致了数据的丢失。

3.如上,当需要读写字符数据的时候,使用字符流,如果不需要读取字符数据的话则使用字节流即可;

4.此处的FileWriter如果想要将数据写入文件,也需要调用flush()方法或者close()方法;

4.文件缓冲输入/输出字符流

BufferedReader和BufferedWriter;

注意:1.BufferedReader 和 BufferedWriter 都是不能直接读取文件数据的类,需要传入参数FileReader和FileWriter来读取文件数据;

2.其他用法类似,注意BufferedReader .readLine()方法和BufferedWriter.newLine()的方法;

BufferedReader相关代码:

BufferedReader reader =  new BufferedReader(new FileReader(new File("G:/files/user.txt"))); 

String getStr = "";

while((getStr = reader.readLine())!=null){

//todo

}

reader.close();

BufferedWriter相关代码:

BufferedWriter  writer =new BufferedWriter(new FileWriter(new File("G:/files/user.txt"),true));

//写入数据

writer.write(str);

//表示换行

writer.newLine();

//关闭输出流,并将数组中的数据写入到文件中去。

writer.close();

end;

0 0
原创粉丝点击