字节流和字符流

本文详细介绍了Java中的IO流概念及分类,包括字节流与字符流的区别和应用场景,并通过实例展示了如何使用各种输入输出流进行文件操作。

1.IO流:将数据从一个地方传输到另一个地方。

2.流的分类:
2.1:按流的方向分:以程序为参考
2.1.1:输入流(读取流):将文件中数据读取到程序中。
2.1.2:输出流(写入流):将程序中数据写入到文件。
2.2:按单位分:
2.2.1:字节流:以字节为单位传输的流。
适用于传输:文本文件,图片,视频,二进制文件。
2.2.2:字符流:以字符为单位传输的流。
适用于传输:文本文件。
2.3:按流的功能:
2.3.1:节点流(基础流):最基本的流。
2.3.2:处理流:处理流封装基本的流。用了装饰者模式。

3.装饰者模式:将基本的类的对象进一步封装成新类,使基本类的功能更强大。

4.字节输入流:InputStream-》FileInputStream
注意:输入流如果文件不存在,会抛出异常。

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	FileInputStream fis=null;
	try {
		/*1.创建流对象*/
		fis=new FileInputStream("aa/a.txt");
		/*2.用流对象读取文件*/
		//准备一个数组
		byte[] b=new byte[10];
		//用流调用方法来读取,返回的是读取的长度
		int len=fis.read(b);
		while (len!=-1) {
			//读一次就输入一次,将读取的内容转换为字符串再输出
			String s1=new String(b, 0, len);
			System.out.println(s1);
			//接着读
			len=fis.read(b);
		}
		System.out.println("读取完毕");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		/*3.关流*/
		if (fis!=null) {
			fis.close();
		}
	}
}

5.字节输出流:OutputStream-》FileOutputStream
注意:输出流的文件不存在,先自动创建文件,再向文件中写入内容。

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	FileOutputStream fos=null;
	try {
		//1.创建流对象,第二个参数决定是否追加
		fos=new FileOutputStream("aa/b.txt",true);
		//2.用流向文件中写入内容
		fos.write("Hello java".getBytes());
		fos.write("dfefiefieif".getBytes());
		System.out.println("写入成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//关流
		if (fos!=null) {
			fos.close();
		}
	}
}

6.字节流的复制:

public static void main(String[] args) throws IOException {
	//声明字节输入流和输出流对象
	FileInputStream fis=null;
	FileOutputStream fos=null;
	try {
		//1.创建流对象
		fis=new FileInputStream("C:\\Users\\sx\\Desktop\\image\\1.jpg");
		fos=new FileOutputStream("aa\\二哈.jpg");
		//2.边读取边写入
		//准备一个数组
		byte[] b=new byte[1024];
		//先读取一次,读取的内容存在数组中,返回读取的长度
		int len=fis.read(b);
		while (len!=-1) {
			//读取一次就写入一次
			fos.write(b, 0, len);
			//接着读
			len=fis.read(b);
		}
		System.out.println("复制成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (fis!=null) {
			fis.close();
		}
		if (fos!=null) {
			fos.close();
		}
	}
}

7.字节缓冲输出流:FilterOutputStream->BufferedOutputStream

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	BufferedOutputStream bos=null;
	try {
		//1.创建流对象,在基本字节流的第二参数可以实现追加
		bos=new BufferedOutputStream(new FileOutputStream("aa/c.txt",true));
		//2.用流对象向文件中写入内容
		bos.write("abcde".getBytes());
		bos.write("bcdef".getBytes());
		System.out.println("写入成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (bos!=null) {
			bos.close();
		}
	}
}

8.字节缓冲输入流:FilterInputStream->BufferedInputStream

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	BufferedInputStream bis=null;
	try {
		//1.创建流对象,在基本字节流的第二参数可以实现追加
		bis=new BufferedInputStream(new FileInputStream("aa/c.txt"));
		//2.用流对象读取文件中的内容
		//准备一个数组
		byte[] b=new byte[10];
		//先读取一次
		int len=bis.read(b);
		while (len!=-1) {
			//将读取的内容转换字符串并输出
			String s1=new String(b, 0, len);
			System.out.println(s1);
			//接着读
			len=bis.read(b);
		}
		System.out.println("读取成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (bis!=null) {
			bis.close();
		}
	}
}

9.所有字符流都是转换流,也是处理流。

10.字符输出流:Writer->OutputStreamWriter

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	OutputStreamWriter osw=null;
	try {
		//1.创建流对象
		osw=new OutputStreamWriter(new FileOutputStream("aa/d.txt",true),"utf-8");
		//2.用流向文件中写入内容
		osw.write("我是千锋人");
		osw.write("我爱千锋");
		osw.append("千锋因你而伟大");
		System.out.println("写入成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (osw!=null) {
			osw.close();
		}
	}
}

11.字符输入流:Reader->InputStreamWriter

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	InputStreamReader isr=null;
	try {
		//1.创建流对象
		isr=new InputStreamReader(new FileInputStream("aa/d.txt"), "utf-8");
		//2.用流读取文件中的内容
		//准备数组
		char[] c=new char[10];
		//先读取一次,读取内容存在数组中,返回的是读取的长度
		int len=isr.read(c);
		while (len!=-1) {
			//读取一次就输出一次
			String s=new String(c, 0, len);
			System.out.println(s);
			//接着读
			len=isr.read(c);
		}
		System.out.println("读取完毕");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (isr!=null) {
			isr.close();
		}
	}
}

12.字符流的复制

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	InputStreamReader isr=null;
	OutputStreamWriter osw=null;
	try {
		//1.创建流对象
		isr=new InputStreamReader(new FileInputStream("aa/d.txt"), "utf-8");
		osw=new OutputStreamWriter(new FileOutputStream("aa/h.txt"), "utf-8");
		//2.边读取边写入
		//准备一个数组
		char[] c=new char[10];
		//先读取一次
		int len=isr.read(c);
		while (len!=-1) {
			//读取一次,就写入一次
			osw.write(c, 0, len);
			//接着读
			len=isr.read(c);
		}
		System.out.println("复制成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (isr!=null) {
			isr.close();
		}
		if (osw!=null) {
			osw.close();
		}
	}
}

13.富二代字符输入流:优点:使用简单,缺点:不能设置字符编码

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	FileReader fw=null;
	try {
		//1.创建流对象
		fw=new FileReader("aa/k.txt");
		//2.用流读取文件中的内容
		//准备一个数组
		char[] c=new char[10];
		//先读取一次,读取的内容存在数组c中,返回是读取的长度
		int len=fw.read(c);
		while (len!=-1) {
			//读取一次就输出一次
			String s1=new String(c, 0, len);
			System.out.println(s1);
			//接着读取
			len=fw.read(c);
		}
		System.out.println("读取成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (fw!=null) {
			fw.close();
		}
	}
}

14.富二代字符输出流:优点:使用简单,缺点:不能设置字符编码

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	FileWriter fw=null;
	try {
		//1.创建流对象
		fw=new FileWriter("aa/k.txt",true);
		//2.用流调用方法向文件中写入内容
		fw.write("大家看过明日之子");
		fw.write("我是中国人");
		fw.append("我爱中国");
		System.out.println("写入成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (fw!=null) {
			fw.close();
		}
	}
}

15.字符缓冲输入流

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	BufferedReader br=null;
	try {
		//1.创建流对象
		br=new BufferedReader(new InputStreamReader(new FileInputStream("aa/u.txt"), "utf-8"));
		//2.读取文件中的内容
		//先读取一行
		String content=br.readLine();
		while (content!=null) {
			//读取一次,就输出一次
			System.out.println(content);
			//接着读
			content=br.readLine();
		}
		System.out.println("读取成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (br!=null) {
			br.close();
		}
	}
}

16.字符缓冲输出流

eg:public static void main(String[] args) throws IOException {
	//声明流对象
	BufferedWriter bw=null;
	try {
		//1.创建流对象
		bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("aa/u.txt",true), "utf-8"));
		//2.向文件中写入内容
		bw.write("先知道有这个知识存在");
		//换行
		bw.newLine();
		bw.write("再了解这个知识是什么");
		//换行
		bw.newLine();
		bw.append("这个知识干嘛用的");
		//换行
		bw.newLine();
		bw.append("这个知识在什么时候用");
		//换行
		bw.newLine();
		System.out.println("写入成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		//3.关流
		if (bw!=null) {
			bw.close();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值