java基础44 IO流技术(输出字节流/缓冲输出字节流)和异常处理

时间:2022-08-04 15:25:54

一、输出字节流

输出字节流的体系:
 -------| 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、实例

 package com.dhb.file;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author DSHORE / 2018-7-2
*
*/
public class Demo10 {
public static void main(String[] args) throws IOException {
writeTest3();
} //方式一:每次只能写入一个字节的数据出去
public static void writeTest1() throws IOException{
//找到目标文件
File file=new File("F:\\a.txt");
//建立数据的输出通道
FileOutputStream fos=new FileOutputStream(file);
//把数据写出(把程序中的数据写到硬盘的文件中)
fos.write('h');
fos.write('e');
fos.write('l');
fos.write('l');
fos.write('o');
//关闭资源
fos.close();
} //方式二:
public static void writeTest2() throws IOException{
//找到目标文件
File f=new File("F:\\a.txt");
//建立数据的输出通道
FileOutputStream fos=new FileOutputStream(f,true);//true:代表在原文件中追加写入当前数据;如果没有true,则原文件中的数据将被当前写入的数据所替代
//把数据写出(把程序中的数据写到硬盘的文件中)
String s="hello worlds";
fos.write(s.getBytes());
//关闭资源
fos.close();
} //方式三:
public static void writeTest3() throws IOException{
//找到目标文件
File file=new File("F:\\a.txt");
//建立数据的输出通道
FileOutputStream fos=new FileOutputStream(file,true);
//把数据写出(把程序中的数据写到硬盘的文件中)
String s="\r\nabc";// \r\n表示:换行,如果只用一个\r或\n,那么只会在第二行追加数据,而不是在下一行添加数据
byte[] buf=s.getBytes();//是将一个字符串转化为一个字节数组。
int i=buf.length;
fos.write(buf,,i);//0:从字节数组指定的索引值开始, 3:写出3个字节
//关闭资源
fos.close();
}
}

二、缓冲输出字节流

BufferedOutputStream出现的目的:是为了提高写数据的效率,内部维护了一个8kb的字节数组而已

2.1、BufferedOutputStream的步骤

1.找到目标文件
    2.建立数据输入输出通道
    3.建立缓冲输入输出流
    4.把数据写出(把程序中的数据写到硬盘中)
    5.关闭资源

2.2、BufferedOutputStream要注意的细节

1.使用BufferedOutputStream写数据时候,他的write方法是先把数据写入到内部维护的字节数组中。
    2.使用BufferedOutputStream写数据时候,他的write方法是先把数据写入到内部维护的字节数组中,如果需要把数据真正的写入到硬盘中,需要调用flush或者close方法,或者是内部维护的数组已经填满数据时候.

2.3、实例

例1:

 package com.dhb.file;

 import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author DSHORE / 2018-7-4
*
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
//找到目标文件
File f=new File("F:\\a.txt");
//建立数据输出通道
FileOutputStream fos=new FileOutputStream(f);
BufferedOutputStream bos=new BufferedOutputStream(fos);
//把数据写出
bos.write("helloWorld".getBytes());
//把缓冲数组内部的数据写到硬盘上面
//bos.flush();
//关闭资源
bos.close();//其实close()里面有个flush()方法
}
}

例2:

 package com.dhb.file;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author DSHORE / 2018-7-4
*
*/
//练习:使用缓冲输入输出字节流拷贝一张图片
public class CopyImage {
public static void main(String[] args) throws IOException {
//找到目标文件
File inFile=new File("F:\\HTML\\3.jpg");
File outFile=new File("F:\\3.jpg"); //建立数据输入输出通道
FileInputStream fis=new FileInputStream(inFile);
FileOutputStream fos=new FileOutputStream(outFile); //建立缓冲输入输出流
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos); //边读边写
int length=;
while((length=bis.read())!=-){
bos.write(length);
}
//关闭资源
bos.close();
bis.close();
}
}

附录(异常处理)

例1:(抛异常)

 package com.dhb.file;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author DSHORE / 2018-7-2
*
*/
//需求:从F盘下的MyJavaCode文件夹中拷贝一张图片到F的根目录下
public class Demo11 {
public static void main(String[] args) throws IOException {
//找到目标文件
File inFile = new File("F:\\MyJavaCode\\20180702.jpg");
File destFile = new File("F:\\20180702.jpg");//把图片复制到这个路径下(必须得写上图片的文件名,否则报异常) //建立数据传输通道
FileInputStream fis = new FileInputStream(inFile);//读
FileOutputStream fos = new FileOutputStream(destFile);//写 //建立缓冲数据,边读边写
byte[] buf = new byte[];
int length = ;
while ((length = fis.read(buf)) != -) {//读
fos.write(buf,,length);//写
}
//关闭资源(先开后关)
fos.close();
fis.close();
}
}

 例2:(捕获异常)

 package com.dhb.file;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* @author DSHORE / 2018-7-4
*
*/
public class Demo13 {
public static void main(String[] args) {
copyImage();
}
public static void copyImage(){
FileInputStream fis=null;
FileOutputStream fos=null;
try {
//找到目标文件
File inFile=new File("F:\\MyJavaCode\\20180702.jpg");
File outFile=new File("F:\\20180702.jpg");//把图片复制到这个路径下(必须得写上图片的文件名,否则报异常)
//建立输入输出通道
fis=new FileInputStream(inFile);//读
fos=new FileOutputStream(outFile);//写
//建立一个缓冲数组,边读边写
byte[] buf=new byte[];
int length=;
while((length=fis.read(buf))!=-){//读
fos.write(buf, , length);//写
}
} catch (IOException e) {
/** e.printStackTrace();//把异常信息打印到控台 */
/*作用:首先阻止throw new RuntimeException(e)下面的代码执行,而且需要通知调用者这里报thow new RuntimeException异常。
并且把IOException传递给RuntimeException包装层,然后抛出,这样子做的目的是为了让调用者使用变得更加灵活。 */
throw new RuntimeException(e);
}finally {//就算上面的代码报异常,下面的代码一定执行
try {//关闭资源(先开后关)
fos.close();
fis.close();
} catch (IOException e) {
/** e.printStackTrace();//把异常信息打印到控台 */
throw new RuntimeException(e);
}
}
}
}

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/9254210.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!