Java IO流的认识 与关系 与使用

IO流

结构图
在这里插入图片描述

1.InputStream OutputStream

原本的抽象基类(都是操作 File(文件路径))
节点流(字节流 byte)

1.创建文件
待读取文件
File file1 = new File("file1.jpg");
待写入文件
File file2 = new File("file2.jpg");
2创建流
输入流
FileInputStream fis = new FileInputStream(file1);
输出流
FileOutputStream fos = new FIleOutputStream(file2);
3操作
读的准备(创建一个 byte[] 数组,与变量 int)
byte[] cbuf = new byte[1024];
int len = 0;
循环进行读取
//read返回读取的数据的长度, -1为没有了
while((len = fis.read(cbuf)) != -1){  
//写入操作
fos.write(cbuf,0,len);
}
finally{
关闭资源
if(fos != null)
fos.close();
if(fis != null)
fis.close();
}

记得抛异常

1.1 BufferedInputStream BufferedOutInputStream

缓冲流 可以理解为 InputStream 与 OutInputStream 的升级版
直接包装一下

1.创建文件
待读取文件
File file1 = new File("file1.jpg");
待写入文件
File file2 = new File("file2.jpg");
2创建流
输入流
BufferedInputStream  fis = new BufferedInputStream(new FileInputStream(file1));
输出流
BufferedOutInputStreamfos = new BufferedOutInputStream(new FIleOutputStream(file2));
3操作
读的准备(创建一个 byte[] 数组,与变量 int)
byte[] cbuf = new byte[1024];
int len = 0;
循环进行读取
//read返回读取的数据的长度, -1为没有了
while((len = fis.read(cbuf)) != -1){  
//写入操作
fos.write(cbuf,0,len);
}
finally{
关闭资源
if(fos != null)
fos.close();
if(fis != null)
fis.close();
}

记得抛异常

2. FileReader FileWriter

原本的抽象基类(都是操作 File(文件路径))
(字符流 char[])

待读取文件
File file1 = new File("file1.txt");
待写入文件
File file2 = new File("file2.txt");

创建流
输入流
FileReader fr = new FileReader(file1);
输出流
FileWriter fw = new FileWriter(file2);

操作

char[] cbuf = new char[20];
int len = 0;

与上面同理
while( (len = fw.reader(cbuf)) != -1){
	sout(new String(cbuf,0,len)); //输出
	写入
	fw.write(cbuf,0,len);
}
finally{
	关闭资源
	if(fr!= null)
	fr.close();
	if(fw!= null)
	fw.close();
}

2.1 BufferedReader BufferWriter

缓冲流 可以理解为 FileReader 与OutInputStream 的升级版

待读取文件
File file1 = new File("file1.txt");
待写入文件
File file2 = new File("file2.txt");

创建流
输入流
BufferedReader fr = new BufferedReader(new FileReader(file1));
输出流
BufferWriterfw = new BufferWriter(new FileWriter(file2));

操作

char[] cbuf = new char[20];
int len = 0;

与上面同理 方法1
while( (len = fw.reader(cbuf)) != -1){
	sout(new String(cbuf,0,len)); //输出
	写入
	fw.write(cbuf,0,len);
}
方法2
String data ;
while((data = fw.readLine()) != null){
fw.write(data);
}
finally{
	关闭资源
	if(fr!= null)
	fr.close();
	if(fw!= null)
	fw.close();
}

2.2 转换流 InputStreamReader OutputStreamWrite

		//创建文件
		File file1 = new File("hellow.txt");
        File file2 = new File("heloow_GBKS.txt");
        //创建流
        FileInputStream fis = new FileInputStream(file1); //这里用的是字节流不是字符流
        FileOutputStream fos = new FileOutputStream(file2);//这里用的是字节流不是字符流
        //用转换流包装
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
        //操作
        char[] cbuf = new char[20];
        int len = 0;

        while((len = isr.read(cbuf))!=-1){
            System.out.println(Arrays.toString(cbuf));
            osw.write(cbuf,0,len);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值