IO流:输入、输出常见的流

IO流:输入、输出常见的流

在这里插入图片描述

在项目中创建aaa.txt和bbb.txt文件


*import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/*
 * 字节流: 音频,视频
 * 		FileInputStream 输入流
 * 		FileOutputStream	输出流
 * 				
 * 				void close()		: 释放资源
 * 				slong skip(long n)		: 跳过几个字节
 * 				汉字的话 字节流的 FileInputStream 读有问题,出现乱码
 *				close方法具备刷新的功能,在关闭之前,就会刷新一次缓冲区,将缓冲区的字节全部刷新到文件上,在关闭
 *				flush方法具备刷新的功能,刷完之后还可以继续写,而close方法刷完之后就不能写了
 *字符流: 可以读取汉字,一般用来读取文本
 *		FileReader
 *		FileWriter
 *				InputStreamReader、OutputStreamWirter 指定用固定的编码表读和写
 *
 *面试题:
 *io流按流向分分为什么?按存储单元分分为什么?
 *输入、输出流		字节流、字符流
 */
public class Stream {
	public static void main(String[] args) throws IOException {
//		 demo1();
//		 demo2();
//		 demo3();
//		 demo4();
//		 demo5();
//		 demo6();
//		 demo7();
//		 demo8();
//	 	 demo9();
//		 demo10();
	}
	
	private static void demo10() throws UnsupportedEncodingException, FileNotFoundException, IOException {
		//使用指定的码表进行读和写,更快,更高效
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("aaa.txt"), "uTf-8"));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("bbb.txt"), "utf-8"));
		int c = 0;
		while((c = br.read()) != -1){
			bw.write(c);
		}
		br.close();
		bw.close();
	}
	
	private static void demo9() throws UnsupportedEncodingException, FileNotFoundException, IOException {
		/*
		 * InputStreamReader、OutputStreamWirter
		 * 使用指定的码表进行读和写
		 */
		InputStreamReader isr = new InputStreamReader(new FileInputStream("aaa.txt"),"uTf-8");
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("bbb.txt"),"gbk");
		int c = 0;
		while((c = isr.read()) != -1){
			osw.write(c);
		}
		isr.close();
		osw.close();
	}
	
	private static void demo8() throws FileNotFoundException, IOException {
		/*
		 * 设置行号的字符流
		 * LineNumberReader
		 */
		LineNumberReader lnr = new LineNumberReader(new FileReader("aaa.txt"));
		String line = "";
		lnr.setLineNumber(100);						//设置行号
		while((line = lnr.readLine()) != null){
			System.out.println(lnr.getLineNumber() + ":" + line); //显示行号
		}
		lnr.close();
	}
	
	private static void demo7() throws FileNotFoundException, IOException {
		/*
		 * 1.6版本io的异常处理
		 */
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("aaa.txt");
			fos = new FileOutputStream("bbb.txt");
			
			int b;
			while((b = fis.read()) != -1) {
				fos.write(b);
			}
		}finally {
			try{
				if(fis != null)
					fis.close();
			}finally {							
				if(fos != null)
					fos.close();
			}
		}
	}
	
	private static void demo6() throws IOException, FileNotFoundException {
		/*
		 * 1.7io流异常处理
		 */
		try (
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aaa.txt"));
				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.txt"));
			) {
			byte[] arr = new byte[3];
			int len;
			while ((len = bis.read(arr)) != -1) {
				bos.write(arr);
			}
		}
	}
	
	private static void demo5() throws FileNotFoundException, IOException {
		/*
		 * 字符缓冲流
		 * 写一行,读一行
		 * 设置char数组的大小不应设置太大,不然后面读的都是空字符,浪费空间
		 */
		BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));
		char[] arr = new char[3];			
		String line;
		while ((line = br.readLine()) != null) { // 写一行, 读一行
			bw.write(line);
			bw.newLine(); // 换行
		}
		br.close();
		bw.close();
	}
	
	private static void demo4() throws FileNotFoundException, IOException {
		/*
		 * 字符流
		 * 字符流定义的数组是char类型的
		 */
		FileReader fr = new FileReader("aaa.txt");
		FileWriter fw = new FileWriter("bbb.txt");
		char[] arr = new char[3];
		int line;
		while ((line = fr.read(arr)) != -1) {
			fw.write(arr);
			System.out.println(new String(arr, 0, line));
		}
		fr.close();
		fw.close();
	}
	
	private static void demo3() throws FileNotFoundException, IOException {
		// 缓冲区的大小为: 8192字节
		// BufferedInputStream:为另一个输入流添加一些功能,在创建BufferedInputStream时,会创建一个内部缓冲区数组,用于缓冲数据。
		// BufferedOutputStream:通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aaa.txt"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb.txt"));
		int len;
		while ((len = bis.read()) != -1) {
			bos.write(len);
			bos.flush();
		}
		bis.close();
		bos.close();
	}
	
	private static void demo2() throws FileNotFoundException, IOException {
		/*
		 * 使用数组,加快read()的速度
		 * 读取:可能会读到半个中文,出现乱码 
		 */
		FileInputStream fis = new FileInputStream("aaa.txt");
		FileOutputStream fos = new FileOutputStream("bbb.txt");
		byte[] arr = new byte[3];
		// 另外一种写法
		// byte[] arr = new byte[fis.available()] 直接设置数组大小为文件大小,一般不用,内存有限
		int len;
		while ((len = fis.read(arr)) != -1) {
			fos.write(len);
			System.out.print(new String(arr,0,len));		//读取:可能会读到半个中文,出现乱码 
		}
		fis.close();
		fos.close();
	}
	
	private static void demo1() throws FileNotFoundException, IOException {
		/*
		 * 字节流 字节流fis.read()的返回值是int类型
		 */
		FileInputStream fis = new FileInputStream("aaa.txt");
		FileOutputStream fos = new FileOutputStream("bbb.txt");
		int len;
		while ((len = fis.read()) != -1) {
			fos.write(len);
		}
		fis.close();
	}
}*

发布了54 篇原创文章 · 获赞 0 · 访问量 347

猜你喜欢

转载自blog.csdn.net/qq_42977003/article/details/102957406
今日推荐