java 读 写 文本文件工具类(按行读取)

package com.test.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 写文件工具
 * @author guishuanglin
 *
 */
public class WriteFileUtils {
	private static Log logger = LogFactory.getLog(WriteFileUtils.class);
	
	private static String fileSplit = "/";

	/**
	 * 写文件
	 * @param in 读取流
	 * @param out 写入流
	 * @return 
	 * @throws Exception
	 */
	public static boolean writeFile(InputStream in, OutputStream out) {
		boolean b = false;
		try {
			int bathSize = 200;
			int i =0;
			long outSize = 0L;
			byte[] bytes = new byte[10240];//10k,根据硬盘缓存大小调节
			int c;
			while ((c = in.read(bytes)) != -1) {
				out.write(bytes, 0, c);
				outSize += c;
				i++;
				if((i>0 && (i% bathSize)== 0) || c <=0) {
					logger.info("正在写文件本次写入: "+ c +" btyes, 总计已写:"+outSize);
				}
			}
			in.close();
			out.close();
		} catch (Exception e) {
			logger.error("写入文件时失败,可能磁盘已满,或者其它原因.", e);
		}
		
		return b;
	}
	
	/**
	 * 写文件
	 * @param srcName 源文件名称,不包含路径
	 * @param destName 目标文件名称,不包含路径
	 * @param srcDir 源目录
	 * @param destDir 目标目录
	 * @param srcEncode 源字符集
	 * @param destEncode 目标字符集
	 * @return
	 */
	public static boolean writeFile(String srcName, String destName, String srcDir, String destDir, String srcEncode, String destEncode) {
		boolean b = false;
		List srcList = readFileData(srcName, srcDir, srcEncode);
		if(srcList == null || srcList.size() ==0) {
			return b;
		}
		b = writeFile(srcList, destName, destDir, destEncode);
		return b;
	}
	
	public static boolean writeFile(List srcList, String destName, String destDir, String destEncode) {
		boolean b = false;
		FileOutputStream out = null;
		try {
			if(srcList==null || srcList.size()==0) {
				return true;
			}
			String outPathFileName = destDir + fileSplit +destName;
			out = getFileOutStream(destName, destDir);
			if(out ==null) {
				return false;
			}
			StringBuffer bf = new StringBuffer(128);
			int bathSize = 200;
			long outSize = 0L;
			byte[] bytes = new byte[20480];//20k,根据硬盘缓存大小调节
			byte[] inbytes = null;
			for(int i=0; i0 && (i% bathSize)== 0) || (i+1) ==srcList.size()){
					//不管原来什么编码, 把字符按目标编码换
					inbytes = bf.toString().getBytes(destEncode);
					ByteArrayInputStream in = new ByteArrayInputStream(inbytes);
					int c;
					while ((c = in.read(bytes)) != -1) {
						out.write(bytes, 0, c);
						outSize += c;
					}
					in.close();
					in = null;
					bf = new StringBuffer(128);
					logger.info("正在写文件["+outPathFileName+"],本次写入: "+inbytes.length+" btyes, 总计已写:"+outSize);
				}
			}
			bytes =null;
			inbytes =null;
			return true;
		} catch (Exception e) {
			b = false;
			logger.error("写入文件时失败,可能磁盘已满,或者其它原因.", e);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				out = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return b;
	}
	
	/**
	 * 读取参数文件文体内容
	 * @param destName 目标文件名称,不包含路径
	 * @param destDir 目标目录
	 * @param destEncode 目标字符集
	 */
	public static List readFileData(String destName, String destDir, String destEncode) {
		List result = null;
		FileInputStream inStream = null;
		BufferedReader bufReader = null;
		String inPathFileName = destDir + fileSplit +destName;
		try {
			inStream = getFileInStream(destName, destDir);
			if(inStream ==null) {
				return null;
			}
			bufReader = new BufferedReader(new InputStreamReader(inStream, destEncode));
			String read = null;
			result = new ArrayList();
			while ((read = bufReader.readLine()) != null) {
				result.add(read);
			}
		} catch (Exception e) {
			result = null;
			logger.error("读取文件失败,"+inPathFileName, e);
		} finally {
			try {
				if (bufReader != null) {
					bufReader.close();
				}
				bufReader = null;
				if (inStream != null) {
					inStream.close();
				}
				inStream = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		logger.info("读取文件行数: "+ (result ==null? 0 :result.size()) +", "+inPathFileName);
		return result;
	}
	
	/**
	 * 没有目录则创建
	 */
	public static boolean mkDirs(String srcDir) {
		boolean b = false;
		File dir = new File(srcDir);
		if (! dir.exists()) {
			b = dir.mkdirs();
		}else {
			b = true;
		}
		return b;
	}
	
	/**
	 * 删除文件
	 */
	public static boolean deleteFile(String srcName, String srcDir) {
		boolean b = false;
		String dirFileName = srcDir + fileSplit +srcName;
		File outf = new File(dirFileName);
		if (outf.exists()) {
			b = outf.delete();
		}else {
			b = true;
		}
		return b;
	}
	
	/** 获取输出文件流 */
	public static FileOutputStream getFileOutStream(String srcName, String srcDir) {
		FileOutputStream out = null;
		try {
			String pathFileName = srcDir + fileSplit +srcName;
			if(! mkDirs(srcDir)) {
				return null;
			}
			out = new FileOutputStream(pathFileName);
		} catch (Exception e){
			out = null;
		}
		//FileUtils.moveFile(srcFile, destFile);
		return out;
	}
	
	/** 获取读取文件流 */
	public static FileInputStream getFileInStream(String srcName, String srcDir) {
		FileInputStream in = null;
		try {
			String pathFileName = srcDir + fileSplit +srcName;
			File inf = new File(pathFileName);
			if ((inf == null) || (! inf.exists() )) {
				return null;
			}
			in = new FileInputStream(pathFileName);
		} catch (Exception e){
			in = null;
		}
		return in;
	}
	
}

这个没有什么说的, 工具类, 很常用.

你可能感兴趣的:(java)