10.2.1 写IO流程序InputStream&&OutputStream&&文件的拷贝

四个抽象类:
在这里插入图片描述

InputStream的几个方法:
close():关闭此输入流并释放与流相关联的任何系统资源;
read():从输入流读取数据的下一个字节;蚂蚁搬家
read(byte[] b):从输入流中读取一些字节数,并将它们存储到缓冲器阵列b;搬家公司
OutputStream的几个方法:
close():关闭此输出流并释放于此流相关联的任何系统资源;
flush():刷新此输出流并强制任何缓冲的输出字节被写出;
write(int b):将指定的字节写入次输出流;
wirte(byte[] b):将b.length字节从指定的字节数组写入此输出流;
write(byte[] b,int off,int len):从指定的字节数组写入len字节,从偏移量off开始输出到此输出流;

##########################文件的读取##########################

1 创建源 2 选择流 3 操作 4 释放
首先创建一个txt文件
在这里插入图片描述
下面这个程序需要记住噢

package com.sxt.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IOTest10{
	public static void main(String[] args) {
		File src=new File("han.txt"); //创建源
		InputStream io=null;
		try {
			io=new FileInputStream(src);  //选择流
			int data;
			while((data=io.read())!=-1)  //读取数据
			{
				System.out.println((char)data);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			try {
				io.close();  //释放
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

输出结果为:
f
i
f
t
h
上面是一个一个的读取,下面试用一卡车一卡车的拉;
FileInputStream: 通过字节的方式读取文件,适合读取所有类型的文件(图像、视频等),全字符请考虑FileReader
FileOutputStream: 通过字节的方式写出或追加所有类型的文件(图像、视频等),全字符请考虑FileWriter
查阅API可以发现如下内容:
int______read()______从该输入流读取一个字节的数据
int______read(byte[] b)从该输入流读取最多b.length个字节的数据到一个字节数组
读到缓冲里面然后从里面拿

byte[] flush=new byte[1024];  //缓冲容器
int len=-1;  //接受长度
while((len=is.read(flush))!=-1)
{
	//字符数组------->字符串
	String str=new String(flush,0,len);
	System.out.print(str);
}

这一段是关键,1024个字节的读,下面是完整代码

package com.sxt.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 四个步骤:分段读取
 * 1.创建源 2.选择流 3.操作 4.释放资源
 * @author 韩文韬
 *
 */
public class IOTest03 {	
	public static void main(String[] args) {
		//1.创建源
		File src=new File("abc.txt");
		//2.选择流
		InputStream is=null;
		try {
			is=new FileInputStream(src);
			//3.操作(读取)
			byte[] flush=new byte[1024];  //缓冲容器
			int len=-1;  //接受长度
			while((len=is.read(flush))!=-1)
			{
				//字符数组------->字符串
				String str=new String(flush,0,len);
				System.out.print(str);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				if(is!=null)
				{
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}

##########################文件的录入##########################

1.创建源 2.选择流 3.写出内容 4.释放
下面是完整代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 文件字节输出流
 * 1.创建源 2.选择流 3.操作(写出内容) 4.释放资源
 * @author 韩文韬
 *
 */
public class IOTest04 {
	public static void main(String[] args) {
		//1.创建源
		File dest=new File("dest.txt");
		//2.选择流
		OutputStream os=null;
		try {
			os=new FileOutputStream(dest,true); //后面的true或false表示是否追加文件
			//3.操作(写出)
			String msg="i love hanwentao\r\n";  //换行
			byte[] datas=msg.getBytes();  //字符串--->字符数组(编码)
			os.write(datas, 0, datas.length);
			os.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				if(os!=null)
				{
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

##########################文件的拷贝##########################

原理上就是将上述两个过程组合起来
在这里插入图片描述

相当于将读取与录入过程结合起来,下面是完整代码:

package com.sxt.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件拷贝:文件字节输入、输出流
 * @author 韩文韬
 *
 */
public class copy {
	public static void main(String[] args) {
		copy("han.txt","wen.txt");
	}
	/**
	 * 文件的拷贝
	 * 思考:利用递归进行文件夹的拷贝
	 * @param hanPath
	 * @param wenPath
	 */
	public static void copy(String hanPath,String wenPath) {
		//1.创建源
		File src=new File(hanPath);  //源头
		File dest=new File(wenPath); //目的地
		//2.选择流
		InputStream is=null;
		OutputStream os=null;
		try {
			is=new FileInputStream(src);
			os=new FileOutputStream(dest,true);
			//3.操作(分段读取)
			byte[] flush=new byte[1024]; //缓冲容器
			int len=-1;                  //接受长度
			while((len=is.read(flush))!=-1)
			{
				os.write(flush, 0, len); //分段写出
			}
			os.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			//4.释放资源 分别关闭 先打开的后关闭
			try {
				if(os!=null)
				{
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(is!=null)
				{
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
}

需要注意的是,不仅仅可以拷贝文字,图片也可以拷贝哦 >

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值