Java字节流和字符流学习和笔记——黑马训练营

-------Java培训、Android培训、iOS培训、.Net培训、期待与您交流!-------



Java API中,对数据的操作是通过流的方式,然而操作流数据分为:字节流和字符流

按流向分为:输出流和输入流
按照字符流分为:Reader、Writer(主要是操作纯文本文件)
按照字节流分为:字节输入流InputStream、和字节输出流OutputStream(非文本文件,比如:图片、音乐、视频....)


1.字节流操作图片

 
//使用流复制非纯文本文件
//
	public static void main(String[] args) {
		
		FileInputStream fis  =null;
		FileOutputStream fos=null;
		try {
			//字符输入流(需要copy文件源)   
			 fis  =new FileInputStream("path路径源");
			//字符输出流(copy存放的目的地)
			 fos= new FileOutputStream("copy目的地");
			//为了提高字符流缓冲技术,所以需要使用
			//字符流缓冲区:BuffereInputStream BuffereInputStream
			BufferedInputStream bfis=new BufferedInputStream(fis);
			BufferedOutputStream bfos=new BufferedOutputStream(fos);
			//读取流数组为1024个字节
			byte[] buf = new byte[1024];
			int len = 0;
			//字节流判断读取数据是否读完,如果读完这返回-1
			while ((len = bfis.read(buf)) != -1) {
				/*写到目的文件————>第一个参数:把数据写到缓存数组。 
								第二个参数:从0个字节开始写
								第三个参数:写到文件末尾
				*/
				bfos.write(buf, 0, len);
			}

		} catch (IOException e1) {
			//抛出异常
			throw new RuntimeException("读取文件失败");
		} finally {
			//在使用IO流的时候很容易产生IOException异常,所以每次用完流之后一定要close();
			try {
				if(fis!=null){
					fis.close();
				}
			} catch (IOException e2) {
				throw new RuntimeException("关闭字节输入流失败");
			}
			try {
				if(fos!=null){
					fos.close();
				}
			} catch (IOException e3) {
				throw new RuntimeException("关闭字节输出流失败");
			}
		}
	}

因为操作的是非纯文本文件,所以使用InputStreamOutpurStream中的FileInputStream、FileOutputStream对象操作图片文件


2.字符流操作文件

public static void main(String[] args) {
		//使用流复制纯文本文件
		// 因为操作的是纯文本文件,所以使用Reader、Writer中的FileReader、FileWriter对象纯文本文件
		FileReader fir = null;
		FileWriter fiw = null;
		BufferedReader bufr = null;
		BufferedWriter bufw = null;
		try {
			// 字节读取流(需要copy文件源)
			fir = new FileReader("path路径源");
			// 字节写入流(copy存放的目的地)
			fiw = new FileWriter("path路径目的地");
			// 为了提高字符流缓冲技术,所以需要使用
			// 字符流缓冲区:BufferedReader BufferedWriter
			bufr = new BufferedReader(fir);
			bufw = new BufferedWriter(fiw);
			// 读取流数组为1024个字节
			byte[] buf = new byte[1024];
			String len = null;
			// 字节流判断读取数据是否读完,如果读完这返回null
			while ((len = bufr.readLine()) != null) {
				//写到目的文件
				bufw.write(len);

			}
		} catch (IOException e1) {
			// 抛出异常
			throw new RuntimeException("读取文件失败");
		} finally {
			// 在使用IO流的时候很容易产生IOException异常,所以每次用完流之后一定要close();
			try {
				if (bufr != null) {
					bufr.close();
				}
			} catch (Exception e2) {
				throw new RuntimeException("关闭字节输入流失败");
			}
			try {
				if (bufw != null) {
					bufw.close();
				}
			} catch (Exception e3) {
				throw new RuntimeException("关闭字节输出流失败");
			}

		}

	}

因为操作的是纯文本文件,所以使用Reader、Writer中的FileReader、FileWriter对象纯文本文件

3.关于Properties配置信息

Properties是Hashtable的子类
也就是说它具备Map集合的特点,而且它里面存储的键值对都是字符串,数据格式:键=值
  是集合和IO技术相结合的集合容器
  对象特点:可以用于键值对形式的配置文件

//设置和获取元素
	public static void main(String[] args) {
		//创建Properties对象并导包import java.util.Properties;
		Properties prop=new Properties();
		//设置键值setProperty(键,值);
		prop.setProperty("zhangsan", "11");
		prop.setProperty("lisi", "12");
		
		//读取某个键的值
		System.out.println(prop.getProperty("lisi"));
		//
		Set<String> names=prop.stringPropertyNames();
		for(String s:names){
			System.out.println(s+":"+prop.getProperty(s));
		}
	}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值