Java之IO(二)缓冲流、转换流

package com.shuhuadream.buffer;

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

import com.shuhuadream.inputstream.IOHelper;


/**
 * 处理流的好处:
 * 处理流必须在节点流的基础之上,增加了效率,提高了性能,扩大的功能。
 * 
 * 1.缓冲流
 *     缓冲字节流
 *         BufferedInputStream
 *         BufferedOutputStream
 *     缓冲字符流
 *         BufferedReader
 *         BufferedWriter
 *处理流内部包含了节点流,节点流决定了与其沟通的外部设备,而处理流则增加了其功能。
 *     
 * 缓冲流的好处:
 *    缓冲流内部包含一个缓冲区域,默认8kb,每一次程序调用read方法其实都是从缓冲区域当中读取内容,如果读取失败
 *    就说明缓冲区域当中没有内容,那么就从数据源当中读取内容,然后会尽可能读取更多的字节放入到缓冲区域当中,
 *    最后缓冲区域当中的内容,会全部返回给程序。
 * 从缓冲区读取数据会比直接从数据源读取数据的速度快,效率也更高,性能更好。
 * 
 * 处理流处理数据和节点流处理数据的方法基本上完全相同。
 * */
public class Demo01 {

	public static void main(String[] args) {
		
//		1.选择流
		FileInputStream fis  = null;
		BufferedInputStream bis = null;
		try {
			fis = new FileInputStream(new File("d:\\d.txt"));
			 bis = new BufferedInputStream(fis);
//			 2.创建缓存字节数组
			 byte[]buf = new byte[1024];
//			 3.定义变量,实际保存的字节数
			 int hasRead = 0;
//			 4.循环读取
			 while ((hasRead = bis.read(buf))!=-1) {
//				 5.操作字节数组
				 String msg = new String(buf, 0,hasRead);
				 System.out.println(msg);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
//			6.关闭流
			IOHelper.closeIO(new Closeable[]{fis,bis});
		}
	}
}
package com.shuhuadream.buffer;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.shuhuadream.inputstream.IOHelper;
public class Demo02 {
	public static void main(String[] args) {
//		1.选择流
		BufferedOutputStream bos = null;
		try {
			bos = new BufferedOutputStream(new FileOutputStream("readme.txt"));
//			2.准备数据源
			String msg = "床前明月光,疑是地上霜。\n举头望明月,低头思故乡。";
			byte[] data = msg.getBytes();
			
//			3.使用输出流写入数据
			bos.write(data);
//			4.刷新流
			bos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
//			5.关闭流
			IOHelper.closeIO(bos);
		}
	}
}
package com.shuhuadream.buffer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.shuhuadream.inputstream.IOHelper;


/**了解:
 * BufferedReader
 * BufferedWriter
 * */
public class Demo03 {

	public static void main(String[] args) {
		File srcFile = new File("C:\\Users\\Administrator\\Desktop\\2017-08-16\\country.txt");
		
		File destFile = new File("country.txt");
		
		copyFile(srcFile, destFile);
		
	}
	
	public static void copyFile(File srcFile,File destFile){
//		1.
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(srcFile));
			bw = new BufferedWriter(new FileWriter(destFile));
//			2.
			String line = "";
//			3
			while (true) {
				line = br.readLine();
				if (line==null) {
					break;
				}
				bw.write(line);
				bw.newLine();   //换行
			}
//			5
			bw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			IOHelper.closeIO(new Closeable[]{br,bw});
		}
	}
}
package com.shuhuadream.buffer;

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

import com.shuhuadream.inputstream.IOHelper;
import com.shuhuadream.inputstream.Test01;
// 缓冲流复制文件
public class Test02 {
	public static void main(String[] args) {
		File srcFile = new File("D:\\360Downloads\\2.文件流和缓冲流.avi");
		
		File destFile1 = new File("D:\\360\\节点流复制文件.avi");
		long time1 = System.currentTimeMillis();
		Test01.copyFile(srcFile, destFile1);
		long time2 = System.currentTimeMillis();
		System.out.println("使用节点流复制文件消耗了:"+(time2-time1)+"毫秒");
		
		File destFile2 = new File("D:\\360\\处理流复制文件.avi");
		long time3 = System.currentTimeMillis();
		copyFile(srcFile, destFile2);
		long time4 = System.currentTimeMillis();
		System.out.println("使用处理流复制文件消耗了:"+(time4-time3)+"毫秒");
	}
	
	public static void copyFile(File srcFile ,File destFile){
//		1.
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			bos = new BufferedOutputStream(new FileOutputStream(destFile));
//			2.
			byte[]buf = new byte[1024];
//			3
			int hasRead = 0;
//			4.
			while ((hasRead = bis.read(buf))!=-1) {
//				5
				bos.write(buf, 0, hasRead);
			}
//			6.
			bos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			IOHelper.closeIO(new Closeable[]{bis,bos});
		}
	}
}
package com.shuhuadream.demo03;

import java.io.UnsupportedEncodingException;

//转换流
/*
 * 转换流作用:把字节流转换为字符流,可以解决出现的因为编码和解码造成的乱码问题
 * InputStreamReader
 * OutputStreamWriter
 * 
 * 编码:字符--编码字符集-----二进制
 * 解码:二进制---解码字符集----字符
 * 
 * 在处理文件时,如果文件的字符格式和编译器处理格式不一样,会出现乱码问题。
 * 
 * 出现乱码问题的原因:
 * 1.编码和解码字符集不一致造成的乱码
 * 2.字节的缺失,长度的丢失
 * 
 * 大部分情况下,出现乱码问题是因为中国汉字,因为中国汉字在不同的字符编码当中占据的字节数不相同,
 * 但是都占据多个字节,而英文没有这个问题。
 */
public class Demo01 {

		public static void main(String[] args) {
//			cover();
			
			String msg = "是中国人就转起来!!";
			byte[] data = msg.getBytes();
			String result = new String(data, 0, 5);
			System.out.println(result);
		}
		public static void cover(){
			String msg = "稻花香里说丰年,听取蛙声一片,呱呱呱!!!";
//			字符串编码的过程,使用编译器默认的格式进行编码
			byte[] data = msg.getBytes();
//			解码
			String result = new String(data, 0, data.length);
			System.out.println(result);
			
//			使用特定的编码集进行编码
			try {
				byte[] darr = msg.getBytes("utf-8");
				
				result = new String(darr,0,darr.length);
				System.out.println(result);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			
		}
}
package com.shuhuadream.demo03;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import com.shuhuadream.inputstream.IOHelper;


// InputStreamReader :转换输入流--》将字节输入流转换成字符输入流
// 作用:为了防止文件使用字符输入流处理时出现乱码问题。
public class Demo02 {
	public static void main(String[] args) {
		File file = new File("C:\\Users\\Administrator\\Desktop\\2017-08-16\\chat.txt");
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader reader = null;
		try {
			 fis = new FileInputStream(file);
			 isr = new InputStreamReader(fis,"utf-8");
			 reader = new BufferedReader(isr);
			 String line = "";
			while ((line = reader.readLine())!=null) {
				System.out.println(line);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			IOHelper.closeIO(new Closeable[]{fis,isr,reader});
		}
	}
}
package com.shuhuadream.demo03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com.shuhuadream.inputstream.IOHelper;
/**
 * 转换流复制文件
 * */
public class Demo03 {
	public static void main(String[] args) {
		File srcFile = new File("C:\\Users\\Administrator\\Desktop\\2017-08-16\\chat.txt");
		File destFile = new File("chat.txt");
		
		copyFile(srcFile, destFile, "UTF-8");
	}
	public static void copyFile(File srcFile,File destFile,String charsetName){
//		1.选择流
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), charsetName));
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile),charsetName));
//			2
			String line = null;
//			3
			while ((line = br.readLine())!=null) {
				bw.write(line);
				bw.newLine();
			}
//			4
			bw.flush();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			IOHelper.closeIO(new Closeable[]{br,bw});
		}
	}
}

你可能感兴趣的:(Java)