模板模式解决流关闭的问题

  每次用到流的时候,总是要处理流的打开和关闭,而这些都是固定的,我们只需要关心文件的处理过程即可,因此使用模板模式可以解决这个问题。下面只是处理文件流的模板,其他的可自行解决:

模板处理写文件

// 写文件
public class OutputStreamProcessingTemplate {
	/**
	 * 缓冲流写文件,默认编码utf-8
	 * 
	 * @param fileName
	 *            文件名
	 * @param append
	 *            true=追加,false=覆盖
	 * @param processor
	 *            处理类
	 * @throws IOException
	 *             异常
	 */
	public static void process(String fileName, boolean append, OutputStreamProcessor processor) throws IOException {
		process(fileName, null, append, processor);
	}

	/**
	 * 缓冲流写文件
	 * 
	 * @param fileName
	 *            文件名
	 * @param fileEncoding
	 *            文件编码
	 * @param append
	 *            true=追加,false=覆盖
	 * @param processor
	 *            处理类
	 * @throws IOException
	 *             异常
	 */
	public static void process(String fileName, String fileEncoding, boolean append, OutputStreamProcessor processor)
			throws IOException {
		IOException processException = null;
		BufferedWriter writer = null;
		try {
			Charset charset = Charset.forName("UTF-8");
			if (fileEncoding != null) {
				charset = Charset.forName(fileEncoding);
			}
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, append), charset));
			processor.process(writer);
		} catch (IOException e) {
			processException = e;
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					if (processException != null) {
						throw new IOException("process " + fileName + " occurs exception.", processException);
					} else {
						throw new IOException("close stream for " + fileName + " occurs exception.", e);
					}
				}
			}
			if (processException != null) {
				throw new IOException("process " + fileName + " occurs exception.", processException);
			}
		}

	}

	public static interface OutputStreamProcessor {
		default public void process(BufferedWriter writer) throws IOException {
		}
	}

	public static void main(String[] args) throws IOException {
		OutputStreamProcessingTemplate.process("file", "UTF-8", true, new OutputStreamProcessor() {
			@Override
			public void process(BufferedWriter writer) throws IOException {
				writer.write("asdasd\n");
				writer.write("asdasd\n");
				writer.write("asdasd阿打算\n");
			}
		});
	}
}

模板处理读文件

// An highlighted block
package com.util.stream;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 输入流处理异常模板
 * 
 * @author 王如雨
 * @createtime 2018年12月18日 下午1:39:26
 */
public class InputStreamProcessingTemplate {
	/**
	 * 缓冲流处理文件,默认编码UTF-8
	 * 
	 * @param fileName
	 *            文件名
	 * @param processor
	 *            处理类
	 * @throws IOException
	 *             异常
	 */
	public static void process(String fileName, InputStreamProcessor processor) throws IOException {
		process(fileName, null, processor);
	}

	/**
	 * 缓冲流处理文件
	 * 
	 * @param fileName
	 *            文件名
	 * @param fileEncoding
	 *            文件编码
	 * @param processor
	 *            处理类
	 * @throws IOException
	 *             异常
	 */
	public static void process(String fileName, String fileEncoding, InputStreamProcessor processor)
			throws IOException {
		IOException processException = null;
		BufferedReader input = null;
		try {
			Charset charset = Charset.forName("UTF-8");
			if (fileEncoding != null) {
				charset = Charset.forName(fileEncoding);
			}
			input = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), charset));
			processor.process(input);
		} catch (IOException e) {
			processException = e;
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					if (processException != null) {
						throw new IOException("process " + fileName + " occurs exception.", processException);
					} else {
						throw new IOException("close stream for " + fileName + " occurs exception.", e);
					}
				}
			}
			if (processException != null) {
				throw new IOException("process " + fileName + " occurs exception.", processException);
			}
		}

	}

	static interface InputStreamProcessor {
		default public void process(BufferedReader input) throws IOException {
		}
	}

	public static void main(String[] args) throws IOException {
		List<String> strs = new ArrayList<>();
		InputStreamProcessingTemplate.process("file", "GBK", new InputStreamProcessor() {
			@Override
			public void process(BufferedReader is) throws IOException {
				String line = null;
				while ((line = is.readLine()) != null) {
					strs.add(line);
				}
			}
		});
		System.out.println(strs);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值