FiltUtil

package com.happyelements.fortuna.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

/*******************************************************************************
 * <p>
 * 普通文件工具类
 * </p>
 * 
 * @author YuanMingLiang
 * @version 1.00 2011/6/29
 * 
 ******************************************************************************/
public class FileUtil {
	private static Logger LOGGER = Logger.getLogger(FileUtil.class);

	/**
	 * 创建文件夹
	 * 
	 * @param path
	 * @return realPath
	 */
	public static String makeDirs(String path) {
		File f = new File(path);
		if (!f.exists()) {
			f.mkdirs();
		}
		return f.getPath();
	}
	
	/**
	 * 在文件末尾追加内容
	 * @param addContent
	 * @param fileName
	 * @param encoding
	 * @return add access or fail
	 */
	public static boolean addWriteStringFile(String addContent, String fileName, String encoding) {
		File file = new File(fileName);
		if (file.isFile() && file.exists()) {
			String s = readStringFile(fileName, encoding);
			addContent = s + addContent;
		}
		
		return writeStringFile(addContent, fileName, encoding);
	}

	/**
	 * 读取文件内容
	 * 
	 * @param fileName
	 * @param encoding
	 * @return
	 */
	public static List<String> readListFile(String fileName, String encoding) {
		List<String> list = new ArrayList<String>();
		try {
			FileInputStream fis = new FileInputStream(fileName);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					fis, encoding));
			while (reader.ready()) {
				String line = reader.readLine();
				list.add(line);
			}
			reader.close();
			fis.close();
		} catch (Exception e) {
			LOGGER.error("读取文件出错:" + fileName, e);
		}
		return list;
	}

	/**
	 * 读取文件内容
	 * 
	 * @param fileName
	 * @param encoding
	 * @return file content
	 */
	public static String readStringFile(String fileName, String encoding) {
		StringBuffer sb = new StringBuffer();
		try {
			FileInputStream fis = new FileInputStream(fileName);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					fis, encoding));
			sb = new StringBuffer();
			while (reader.ready()) {
				String line = reader.readLine();
				sb.append(line);
				sb.append(System.getProperty("line.separator"));
			}
			reader.close();
			fis.close();
		} catch (Exception e) {
			LOGGER.error("读取文件出错:" + fileName, e);
		}
		return sb.toString();
	}

	/**
	 * 将内容写入文件里
	 * 
	 * @param fileContent
	 * @param fileName
	 * @param encoding
	 * @return add access or fail
	 */
	public static boolean writeStringFile(String fileContent, String fileName,
			String encoding) {
		try {
			makeDirs(getFileNamePath(fileName));
			File file = new File(fileName);
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			byte[] b = fileContent.getBytes(encoding);
			fileOutputStream.write(b);
			fileOutputStream.close();
			return true;
		} catch (Exception e) {
			LOGGER.error("写入文件出错:" + fileName, e);
			return false;
		}
	}

	/**
	 * 得到文件路径
	 * 
	 * @param file
	 * @return file real path
	 */
	public static String getFileNamePath(File file) {
		return getFileNamePath(file.getAbsolutePath());
	}

	/**
	 * 得到文件路径
	 * 
	 * @param fileName
	 * @return file real path
	 */
	public static String getFileNamePath(String fileName) {
		int pos = fileName.lastIndexOf("\\");
		int pos2 = fileName.lastIndexOf("/");
		if (pos == -1 && pos2 == -1) {
			return "";
		} else {
			if (pos2 > pos) {
				return fileName.substring(0, pos2);
			} else {
				return fileName.substring(0, pos);
			}
		}
	}

	/**
	 * 读取文件夹中所有文件名(不包括文件夹中的子目录)
	 * 
	 * @param filePath
	 * @return
	 */
	public static List<String> readDirsAllFiles(String filePath) {
		List<String> fileNameList = new ArrayList<String>();
		LOGGER.debug("读取文件路径 " + filePath);
		File f = new File(filePath);
		File[] files = f.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (!files[i].isDirectory()) {
				File file = files[i];
				int pos = file.getName().lastIndexOf(".");
				if (pos < 0) {
					fileNameList.add(file.getName());
				} else {
					fileNameList.add(file.getName().substring(0, pos));
				}
			}
		}
		return fileNameList;
	}

	/**
	 * 删除单个文件
	 * 
	 * @param fileName
	 *            被删除文件的文件名
	 * @return 单个文件删除成功返回true,否则返回false
	 */
	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		if (file.isFile() && file.exists()) {
			file.delete();
			LOGGER.debug("删除单个文件" + fileName + "成功!");
			return true;
		} else {
			LOGGER.error("删除单个文件" + fileName + "失败!");
			return false;
		}
	}

	/**
	 * 
	 * 取得当前的目录的path
	 * 
	 * @return
	 */
	public static String getCanonicalPath() {
		try {
			return new File("").getCanonicalPath();
		} catch (IOException e) {
			LOGGER.error("", e);
		}
		return "";
	}

}



你可能感兴趣的:(util)