一、输出字节流
输出字节流的体系:
-------| OutputStream:所有输出字节流的基类(抽象类)
----------| FileOutputStream:向文件输出数据的输出字节流(把程序中的数据写到硬盘中)
----------| BufferedOutputStream:缓冲输出字节流 BufferedOutputStream出现的目的:是为了提高写数据的效率,内部维护了一个8kb的字节数组而已
注意:所有缓冲流都不具备读写文件的能力(比如BufferedOutputStream,他要借助FileOutputStream的写功能来写文件)
1.1、IO流分类
按照数据的流向划分:
输入流:把硬盘(或内存)中的数据读到程序中。
输出流:把程序中的数据写到硬盘(或内存)中。
按照处理的单位划分:
字节流:字节流读取都是文件中的二进制数据,读取到的二进制数据不会经过任何的处理.
字符流:字符流读取的数据是以字符为单位的;字符流也是读取的是二进制数据,不过会把这些二进制数据转换为我们认识的字符.(字符流=字节流+解码)
1.2、FileOutPutStream的步骤
1.找目标文件
2.建立数据输出通道
3.把数据转换为字节数组写出(把程序中的数据写到硬盘中)
4.关闭资源
1.3、FileOUtPutStream注意的细节
1.使用FileOutPutStream的时候,如果目标文件不存在,那么会自动创建目标对象
2.使用FileOutPutStream的时候,如果目标文件已经存在,那么会先清空目标文件的数据,然后在写入
3.使用FileOutPutStream写数据的时候,如果目标文件已经存在,需要在原来基础上追加数据的时候使用New FileOutPutStream(file,true)构造函数,第二参数为true.
4.使用FileOutPutStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的是一个字节数据,只有把低于八位的二进制写出来,其他的二十四位数据全部被抛弃了
1.4、实例
1 package com.dhb.file;2
3 import java.io.File;4 import java.io.FileOutputStream;5 import java.io.IOException;6
7 /**8 * @author DSHORE / 2018-7-29 *10 */
11 public classDemo10 {12 public static voidmain(String[] args) throws IOException {13 writeTest3();14 }15
16 //方式一:每次只能写入一个字节的数据出去
17 public static voidwriteTest1() throws IOException{18 //找到目标文件
19 File file=new File("F:\\a.txt");20 //建立数据的输出通道
21 FileOutputStream fos=newFileOutputStream(file);22 //把数据写出(把程序中的数据写到硬盘的文件中)
23 fos.write('h');24 fos.write('e');25 fos.write('l');26 fos.write('l');27 fos.write('o');28 //关闭资源
29 fos.close();30 }31
32 //方式二:
33 public static voidwriteTest2() throws IOException{34 //找到目标文件
35 File f=new File("F:\\a.txt");36 //建立数据的输出通道
37 FileOutputStream fos=new FileOutputStream(f,true);//true:代表在原文件中追加写入当前数据;如果没有true,则原文件中的数据将被当前写入的数据所替代38 //把数据写出(把程序中的数据写到硬盘的文件中)
39 String s="hello worlds";40 fos.write(s.getBytes());41 //关闭资源
42 fos.close();43 }44
45 //方式三:
46 public static voidwriteTest3() throws IOException{47 //找到目标文件
48 File file=new File("F:\\a.txt");49 //建立数据的输出通道
50 FileOutputStream fos=new FileOutputStream(file,true);51 //把数据写出(把程序中的数据写到硬盘的文件中)
52 String s="\r\nabc";//\r\n表示:换行,如果只用一个\r或\n,那么只会在第二行追加数据,而不是在下一行添加数据
53 byte[] buf=s.getBytes();//是将一个字符串转化为一个字节数组。
54 int i=buf.length;55 fos.write(buf,0,i);//0:从字节数组指定的索引值开始, 3:写出3个字节56 //关闭资源
57 fos.close();58 }59 }
二、缓冲输出字节流
BufferedOutputStream出现的目的:是为了提高写数据的效率,内部维护了一个8kb的字节数组而已
2.1、BufferedOutputStream的步骤
1.找到目标文件
2.建立数据输入输出通道
3.建立缓冲输入输出流
4.把数据写出(把程序中的数据写到硬盘中)
5.关闭资源
2.2、BufferedOutputStream要注意的细节
1.使用BufferedOutputStream写数据时候,他的write方法是先把数据写入到内部维护的字节数组中。
2.使用BufferedOutputStream写数据时候,他的write方法是先把数据写入到内部维护的字节数组中,如果需要把数据真正的写入到硬盘中,需要调用flush或者close方法,或者是内部维护的数组已经填满数据时候.
2.3、实例
例1:
1 package com.dhb.file;2
3 import java.io.BufferedOutputStream;4 import java.io.File;5 import java.io.FileOutputStream;6 import java.io.IOException;7
8 /**9 * @author DSHORE / 2018-7-410 *11 */
12 public classDemo2 {13 public static voidmain(String[] args) throws IOException {14 //找到目标文件
15 File f=new File("F:\\a.txt");16 //建立数据输出通道
17 FileOutputStream fos=newFileOutputStream(f);18 BufferedOutputStream bos=newBufferedOutputStream(fos);19 //把数据写出
20 bos.write("helloWorld".getBytes());21 //把缓冲数组内部的数据写到硬盘上面22 //bos.flush();23 //关闭资源
24 bos.close();//其实close()里面有个flush()方法
25 }26 }
例2:
1 package com.dhb.file;2
3 import java.io.BufferedInputStream;4 import java.io.BufferedOutputStream;5 import java.io.File;6 import java.io.FileInputStream;7 import java.io.FileOutputStream;8 import java.io.IOException;9
10 /**11 * @author DSHORE / 2018-7-412 *13 */
14 //练习:使用缓冲输入输出字节流拷贝一张图片
15 public classCopyImage {16 public static voidmain(String[] args) throws IOException {17 //找到目标文件
18 File inFile=new File("F:\\HTML\\3.jpg");19 File outFile=new File("F:\\3.jpg");20
21 //建立数据输入输出通道
22 FileInputStream fis=newFileInputStream(inFile);23 FileOutputStream fos=newFileOutputStream(outFile);24
25 //建立缓冲输入输出流
26 BufferedInputStream bis=newBufferedInputStream(fis);27 BufferedOutputStream bos=newBufferedOutputStream(fos);28
29 //边读边写
30 int length=0;31 while((length=bis.read())!=-1){32 bos.write(length);33 }34 //关闭资源
35 bos.close();36 bis.close();37 }38 }
附录(异常处理)
例1:(抛异常)
1 package com.dhb.file;2
3 import java.io.File;4 import java.io.FileInputStream;5 import java.io.FileOutputStream;6 import java.io.IOException;7
8 /**9 * @author DSHORE / 2018-7-210 *11 */
12 //需求:从F盘下的MyJavaCode文件夹中拷贝一张图片到F的根目录下
13 public classDemo11 {14 public static voidmain(String[] args) throws IOException {15 //找到目标文件
16 File inFile = new File("F:\\MyJavaCode\\20180702.jpg");17 File destFile = new File("F:\\20180702.jpg");//把图片复制到这个路径下(必须得写上图片的文件名,否则报异常)18
19 //建立数据传输通道
20 FileInputStream fis = new FileInputStream(inFile);//读
21 FileOutputStream fos = new FileOutputStream(destFile);//写22
23 //建立缓冲数据,边读边写
24 byte[] buf = new byte[1024];25 int length = 0;26 while ((length = fis.read(buf)) != -1) {//读
27 fos.write(buf,0,length);//写
28 }29 //关闭资源(先开后关)
30 fos.close();31 fis.close();32 }33 }
例2:(捕获异常)
1 package com.dhb.file;2
3 import java.io.File;4 import java.io.FileInputStream;5 import java.io.FileOutputStream;6 import java.io.IOException;7
8 /**9 * @author DSHORE / 2018-7-410 *11 */
12 public classDemo13 {13 public static voidmain(String[] args) {14 copyImage();15 }16 public static voidcopyImage(){17 FileInputStream fis=null;18 FileOutputStream fos=null;19 try{20 //找到目标文件
21 File inFile=new File("F:\\MyJavaCode\\20180702.jpg");22 File outFile=new File("F:\\20180702.jpg");//把图片复制到这个路径下(必须得写上图片的文件名,否则报异常)23 //建立输入输出通道
24 fis=new FileInputStream(inFile);//读
25 fos=new FileOutputStream(outFile);//写26 //建立一个缓冲数组,边读边写
27 byte[] buf=new byte[1024];28 int length=0;29 while((length=fis.read(buf))!=-1){//读
30 fos.write(buf, 0, length);//写
31 }32 } catch(IOException e) {33 /** e.printStackTrace();//把异常信息打印到控台 */
34 /*作用:首先阻止throw new RuntimeException(e)下面的代码执行,而且需要通知调用者这里报thow new RuntimeException异常。35 并且把IOException传递给RuntimeException包装层,然后抛出,这样子做的目的是为了让调用者使用变得更加灵活。*/
36 throw newRuntimeException(e);37 }finally {//就算上面的代码报异常,下面的代码一定执行
38 try {//关闭资源(先开后关)
39 fos.close();40 fis.close();41 } catch(IOException e) {42 /** e.printStackTrace();//把异常信息打印到控台 */
43 throw newRuntimeException(e);44 }45 }46 }47 }