java io 缓冲流

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Logger;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author LimingWang
 * @date 2015年4月15日 下午4:55:06
 * @version 1.0
 */
public class IOTest {
	
	private Logger logger = Logger.getLogger("IOTest");
	
	public String separator;
	public String path;
	public OutputStream fos;
	public BufferedOutputStream bos;
	public InputStream fis;
	public BufferedInputStream bis;
	public Writer fw;
	public BufferedWriter bw;
	public Reader fr;
	public BufferedReader br;
	
	/**
	 * 初始化公用内容
	 * <功能详细描述>
	 * @author LimingWang
	 * @since 2015年4月15日 下午4:56:38
	 */
	@Before
	public void testBefore(){
		separator = File.separator;
		path = "C:" + separator + "Users" + separator 
				+ "limingwang" + separator + "Desktop" + separator 
				+ "test.txt";
	}
	
	/**
	 * 关闭流
	 * <功能详细描述>
	 * @author LimingWang
	 * @throws IOException
	 * @since 2015年4月15日 下午4:57:24
	 */
	@After
	public void testAfter() throws IOException{
		if (bos != null) {
			bos.flush();
			bos.close();
		}
		if (fos != null) {
			fos.close();
		}
		
		if (bis != null) {
			bis.close();
		}
		if (fis != null) {
			fis.close();
		}
		
		if (bw != null) {
			bw.flush();
			bw.close();
		}if (fw != null) {
			fw.close();
		}
		
		if (br != null) {
			br.close();
		}
		if (fr != null) {
			fr.close();
		}
	}

	/**
	 * 字节输出缓冲流
	 * <功能详细描述>
	 * @author LimingWang
	 * @throws Exception
	 * @since 2015年4月15日 下午4:58:24
	 */
	@Test
	public void testByteOut() throws Exception {
		fos = new FileOutputStream(path);
		bos = new BufferedOutputStream(fos);
		String data = "test";
		bos.write(data.getBytes());
	}
	
	/**
	 * 字节输入缓冲流
	 * <功能详细描述>
	 * @author LimingWang
	 * @throws Exception
	 * @since 2015年4月15日 下午5:44:14
	 */
	@Test
	public void testByteIn() throws Exception {
		fis = new FileInputStream(path);
		bis = new BufferedInputStream(fis);
		StringBuffer sb = new StringBuffer();
		byte[] bytes = new byte[1024];
		int len = -1;
		while((len = bis.read(bytes)) != -1){
			sb.append(new String(bytes, 0, len));
		}
		logger.info(sb.toString());
	}
	
	/**
	 * 字符输出缓冲流
	 * <功能详细描述>
	 * @author LimingWang
	 * @throws Exception
	 * @since 2015年4月15日 下午5:44:54
	 */
	@Test
	public void testCharOut() throws Exception {
		fw = new FileWriter(path);
		bw = new BufferedWriter(fw);
		bw.write("test2");
	}
	
	/**
	 * 字符输入缓冲流
	 * <功能详细描述>
	 * @author LimingWang
	 * @throws Exception
	 * @since 2015年4月15日 下午5:45:50
	 */
	@Test
	public void testCharIn() throws Exception {
		Reader fr = new FileReader(path);
		BufferedReader br = new BufferedReader(fr);
		char[] ch = new char[1024];
		int len = -1;
		StringBuilder sb = new StringBuilder();
		while((len = br.read(ch)) != -1){
			sb.append(new String(ch, 0, len));
		}
		logger.info(sb.toString());
		br.close();
		fr.close();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值