何为输入流,何为输出流,何为缓冲流

在沈阳华清远见的学习之输入输出流~

文章目录


前言

        流(Stream)是指在计算机的输入输出操作中各部件之间的数据流动。按照数据的传输方向,流可分为输入流与输出流,它是java中比较重要的知识点,无论是面试考试,还是实际工作中,这都是经常能够看到的身影,所以今天我写下这篇文章,希望各位看官可以更好地了解流的相关知识。


1、输入流

1.1字节输入流(InputStream)

        字节流处理单元为1个字节,操作字节和字节数组。

package com.hqyj.test;

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

public class Test {

	public static void main(String[] args) throws IOException {
		File file = new File("文件地址");
		if (file.exists()) {
			FileInputStream fis = null;//判断文件是否存在
			try {
				// 创建一个文件字节流进行对文件读取
				fis = new FileInputStream(file);
				// 创建字节数组作为传递载体
				byte b[] = new byte[1024];
				// 判断每次读取到的内容数量,不为-1则说明还有内容
				int length = 0;
				// 创建一个字符串对象来承载读取到的内容
				StringBuffer str = new StringBuffer();
				// 循环读取,数量为-1则说明读取完成
				while ((length = fis.read(b)) != -1) {
					// 将每次读取到的内容制作成字符串,拼接在原字符串的后面
					str.append(new String(b, 0, length));
				}
				// 显示字符串
				System.out.println(str);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				// 关流
				fis.close();
			}

		} else {
			System.out.println("文件不存在");
		}
	}

}

1.2字符输入流(Reader)

        字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好。

package com.hqyj.test;

import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test {

	public static void main(String[] args) throws IOException {
		File file = new File("文件地址");
		if (file.exists()) {
			FileReader fr = null;//判断文件是否存在
			try {
				// 创建一个文件字节流进行对文件读取
				fr = new FileReader(file);
				// 创建字节数组作为传递载体
				char[] c= new char[1024];
				// 判断每次读取到的内容数量,不为-1则说明还有内容
				int length = 0;
				// 创建一个字符串对象来承载读取到的内容
				StringBuffer str = new StringBuffer();
				// 循环读取,数量为-1则说明读取完成
				while ((length = fr.read(c)) != -1) {
					// 将每次读取到的内容制作成字符串,拼接在原字符串的后面
					str.append(new String(c, 0, length));
				}
				// 显示字符串
				System.out.println(str);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				// 关流
				fr.close();
			}

		} else {
			System.out.println("文件不存在");
		}
	}

}

2、输出流

2.1字节输出流(OutputStream)

package com.hqyj.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) throws IOException {
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		File file = new File("文件地址");
		if (file.exists()) {
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(file,true);
				fos.write(str.getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				fos.close();
			}

		} else {
			System.out.println("文件不存在");
		}
	}

}

2.2字符输出流(Writer)

package com.hqyj.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) throws IOException {
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		File file = new File("文件地址");
		if (file.exists()) {
			FileWriter fw = null;
			try {
				fw = new FileWriter(file,true);
				fw.write(str.getBytes());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} finally {
				fw.close();
			}

		} else {
			System.out.println("文件不存在");
		}
	}

}

3、文件复制

首先要提到的是字节流与字符流的区别:

(1)字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节,操作字节和字节数组。

(2)字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好

(3)如果是音频文件、图片、歌曲,就用字节流好点,如果是关系到中文(文本)的,用字符流好点

(4)所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。在读取文件(特别是文本文件)时,也是一个字节一个字节地读取以形成字节序列

        说完了输入流与输出流,接下来就是他们的应用啦,最经典的例子就是文件复制的问题,这里用字节流举例

package com.hqyj.test;

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

public class Test2 {

	public static void main(String[] args) throws IOException {
		File file = new File("文件1地址");
		File file1 = new File("文件2地址");
		FileInputStream fis = null;
		FileOutputStream fos = null;

		fis = new FileInputStream(file);
		fos = new FileOutputStream(file1);
		byte b[] = new byte[1024];
		int length = 0;
		while ((length = fis.read(b)) != -1) {
			fos.write(b);
		}
		fis.close();
		fos.close();
	}

}

4、缓冲流

字节流:BufferedInputStream,BufferedOutputStream

字符流:BufferedReader,BufferedWritter

        缓冲流是一个特别的流,缓冲流的基本原理是创建流对象时候,会创建一个内置的默认大小的缓冲区数组,通过缓冲区书写,他的作用是提高系统读写速度,这里用上面的文件复制加缓冲流进行举例

package com.hqyj.test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test3 {

	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream(
				new File("文件1地址"));
		FileOutputStream fos = new FileOutputStream(
				new File("文件2地址"));
		BufferedInputStream bis = new BufferedInputStream(fis);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		byte[] bytes = new byte[1024];
		while (bis.read(bytes) != -1) {
			bos.write(bytes);
		}
		bos.flush();
		bis.close();
		bos.close();
		fis.close();
		fos.close();
		

	}

}

        缓冲流减少了CPU通过内存访问磁盘的次数,极大的提高了读写的速度和效率,IO流操作文件内容都是在缓冲流内部的缓冲数组之中的。


总结

        以上就是今天要讲的内容,何为输入流,何为输出流,何为字节流,他们的作用是什么,怎么使用,有什么优缺点,希望对各位读者能有所帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值