File操作工具类

星期天的时间不上班,用了一些时间写了java的操作文件的工具类,代码如下:

package com.sq.shared.util;

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.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;



public class FileUtil {

	/**
	 * @Fields log : log4j句柄
	 */
	protected static Logger log = Logger.getLogger(FileUtil.class);

	

	/**
	 * @Title: readFile
	 * @Description: TODO(读取文件的内容在指定的路径下)
	 * @param @param path
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @date 2016年4月24日 下午3:49:54
	 * @throws
	 */
	public static String readFile(String path) {
		BufferedReader br = null;
		String fileContent = "";
		try {
			br = new BufferedReader(new FileReader(new File(path)));
			String temp = "";
			while ((temp = br.readLine()) != null) {
				fileContent = fileContent + temp;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			if (null != br) {
				try {
					br.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
		return fileContent;
	}

	/**
	 * 
	 * @Title: readPakageFileUTF8
	 * @Description: TODO(读取随意某个包路径下文件)
	 * @param @param filePath
	 * @param @return
	 * @param @throws Exception 设定文件
	 * @return String 返回类型
	 * @author: mxr
	 * @date 2016年4月22日 下午2:55:42
	 * @throws
	 */
	public static String readPakageFileUTF8(String filePath) {
		String fileContent = "";
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		// filePath 例如"com/sq/view/report/singleVehicle/action/chartbg.json"
		InputStream inputStream = null;
		BufferedReader bufferedReader = null;
		try {
			inputStream = classLoader.getResourceAsStream(filePath);
			bufferedReader = new BufferedReader(new InputStreamReader(
					inputStream, "UTF-8"));
			String temp = "";
			while ((temp = bufferedReader.readLine()) != null) {
				fileContent = fileContent + temp;
			}
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		} catch (IOException e) {
			log.error(e.getMessage());
		} finally {
			if (null != bufferedReader) {
				try {
					bufferedReader.close();
					if (null != inputStream) {
						inputStream.close();
					}
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
		return fileContent;
	}

	/**
	 * @Title: copyFile
	 * @Description: TODO(实现文本文件的复制 当isApend为true时可以对文件内容进行追加)
	 * @param @param filePath
	 * @param @param outputPath
	 * @param @param isApend 设定文件
	 * @return void 返回类型
	 * @author: mxr
	 * @date 2016年4月24日 下午2:57:22
	 * @throws
	 */
	public static void copyFile(String filePath, String outputPath,
			String content, boolean isApend) {
		createNewDir(outputPath);
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(new File(filePath)));
			bw = new BufferedWriter(new FileWriter(new File(outputPath),
					isApend));
			String str;// 结尾不等于空
			while ((str = br.readLine()) != null) {
				bw.write(str);
				bw.newLine(); // 重新起一行
				bw.flush();
			}
			if (null != content && StringUtils.isNotEmpty(content)) {
				bw.write(content);
				bw.flush();
			}
		} catch (IOException e) {
			log.error(e.getMessage());
		} finally {
			if (null != bw) {
				try {
					bw.close();
					if (null != br) {
						br.close();
					}
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * 可以实现对非文本文件的复制(例如:视频,图片,等流文件)
	 * 
	 * @Title: copyStreamFile
	 * @Description: TODO(文件的复制)
	 * @param @param filePath
	 * @param @param outputPath
	 * @param @throws Exception 设定文件
	 * @return void 返回类型
	 * @author: mxr
	 * @date 2016年4月24日 下午2:50:33
	 * @throws
	 */
	public static void copyStreamFile(String filePath, String outputPath) {
		createNewDir(filePath);
		// 输入流
		BufferedInputStream bis = null;
		// 输出流
		BufferedOutputStream bos = null;
		try {
			// 2.创建响应的节点流,FileInputStream,FileOutputStream
			bis = new BufferedInputStream(new FileInputStream(
					new File(filePath)));
			bos = new BufferedOutputStream(new FileOutputStream(new File(
					outputPath)));
			// 实现文件的复制
			int len = 0;
			byte[] b = new byte[1024];// lKB=1024B
			while ((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
				// BufferedOutputStream阻塞式的所以需要刷新
				bos.flush();
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			// 关缓冲流就可以了,他会自动关闭牛节点流
			if (null != bos) {
				try {
					bis.close();
					if (null != bis) {
						bis.close();
					}
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * @Title: writeContentFile
	 * @Description: TODO(写入文件内容 isApend 为 true 时 表示追加文件内容 为false覆盖文件内容)
	 * @param @param content
	 * @param @param path
	 * @param @param isApend
	 * @param @throws IOException 设定文件
	 * @return void 返回类型
	 * @author: mxr
	 * @date 2016年4月24日 下午3:05:26
	 * @throws
	 */
	public static void writeContentFile(String content, String path,
			boolean isApend) {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(path, isApend));
			bw.write(content);
			bw.flush();
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			if (null != bw) {
				try {
					bw.close();
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
		}
	}

	/**
	 * @Title: delFileOrDerectory
	 * @Description: TODO(删除文件路径下所有的文件)
	 * @param @param path 设定文件
	 * @return void 返回类型
	 * @author:mxr
	 * @date 2016年4月24日 下午3:12:02
	 * @throws
	 */
	public static void delFileOrDerectory(String path) {
		try {
			File file = new File(path);
			if (file.exists()) {
				if (file.isDirectory()) {
					File[] files = file.listFiles();
					for (int i = 0, len = files.length; i < len; i++) {
						File subFile = files[i];
						delFileOrDerectory(subFile.getAbsolutePath());
					}
					file.delete();
				} else {
					file.delete();
				}
			}
		} catch (Exception e) {
			log.error("删除文件时" + e.getMessage());
		}
	}

	/**
	 * @Title: createNewDir
	 * @Description: TODO(如果文件不存在创建新的文件)
	 * @param @param outputPath 设定文件
	 * @return void 返回类型
	 * @author: mxr
	 * @date 2016年4月24日 下午2:29:40
	 * @throws
	 */
	public static void createNewDir(String outputPath) {
		File outputFile = new File(outputPath);
		File outputDir = outputFile.getParentFile();
		if (!outputDir.exists()) {
			outputDir.mkdirs();
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值