package com.platform.utils;

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 org.apache.log4j.Logger;

/**
 * 文件读写操作帮助类
 * 
 * @author wuming
 * 
 */
public class FileOperateHelper {
	
	private static Logger log = Configs.CONSOLE_LOGGER.getLogger(FileOperateHelper.class);

	/**
	 * 以追加的方式将信息写入文件
	 * 
	 * @param path
	 * @param message
	 */
	public static void fileWrite(String path, String message) {
		if (null == path || "".equals(path)) {
			return;
		}
		try {
			File file = new File(path);
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream out = new FileOutputStream(file, true); // 如果追加方式用true
			StringBuffer sb = new StringBuffer();
			sb.append(message);
			out.write(sb.toString().getBytes("utf-8"));
			out.close();
		} catch (IOException e) {
			log.error(e.getStackTrace());
		}
	}
	
	/**
	 * 以重写的方式将信息写入文件
	 * 
	 * @param path
	 * @param message
	 */
	public static void fileReWrite(String path, String message) {
		if (null == path || "".equals(path)) {
			return;
		}
		FileOutputStream out = null;
		try {
			File file = new File(path);
			file.delete();
			file.createNewFile();
			out = new FileOutputStream(file, true); // 如果追加方式用true
			StringBuffer sb = new StringBuffer();
			sb.append(message);
			out.write(sb.toString().getBytes("utf-8"));
			out.close();
		} catch (IOException e) {
			log.error(e.getStackTrace());
		}
		finally{
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					log.error(e.getStackTrace());
				}
			}
		}
	}
	
	/**
	 * 以重写的方式将信息写入文件
	 * 
	 * @param path
	 * @param message
	 */
	public static void fileReWriteGBK(String path, String message) {
		if (null == path || "".equals(path)) {
			return;
		}
		FileOutputStream out = null;
		try {
			File file = new File(path);
			file.delete();
			file.createNewFile();
			out = new FileOutputStream(file, true); // 如果追加方式用true
			StringBuffer sb = new StringBuffer();
			sb.append(message);
			out.write(sb.toString().getBytes("GBK"));
			out.close();
		} catch (IOException e) {
			log.error(e.getStackTrace());
		}
		finally{
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					log.error(e.getStackTrace());
				}
			}
		}
	}

	/**
	 * 文件读取方法
	 * @param path
	 * @return
	 */
	@SuppressWarnings("resource")
	public static String fileReader(String path) {		
		StringBuffer sb = new StringBuffer();
		String tempString = "";
		FileInputStream fis = null;
		try {
			File file = new File(path);			
			if (!file.exists())
				return "当前没有信息！";
			fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
			while ((tempString = br.readLine()) != null) {
				sb.append(tempString).append("\r\n");
			}
		} catch (Exception e) {
			log.error(e.getStackTrace());
		}
		finally{
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					log.error(e.getStackTrace());
				}
			}
		}
		return sb.toString();
	}
	
	/**
	 * 文件读取方法-GBK
	 * @param path
	 * @return
	 */

	@SuppressWarnings("resource")
	public static String fileReaderAndendline(String path) {

		StringBuffer sb = new StringBuffer();
		String tempString = "";
		FileInputStream fis = null;
		try {
			File file = new File(path);
			if (!file.exists())
				return "";
			fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis,"GBK"));
			while ((tempString = br.readLine()) != null) {
				sb.append(tempString+"\n");
			}
		} catch (Exception e) {
			log.error(e.getStackTrace());
		}
		finally{
			if (null != fis) {
				try {
					fis.close();
				} catch (IOException e) {
					log.error(e.getStackTrace());
				}
			}
		}
		return sb.toString();
	}
	
	public static String addLastSeparator(String path){
		//去掉  File.separator
		path = removeLastSeparator(path);
		// 末尾 加上 /
		path = path + File.separator;
		return path;
	}
	
	public static String addLastLinuxSeparator(String path){
		//去掉  File.separator
		path = removeLastLinuxSeparator(path);
		// 末尾 加上 /
		path = path + "/";
		return path;
	}
	
	public static String removeLastSeparator(String path){
		// 递归去掉 所有 结尾 /
		if (path.length() < 1) {
			return path;
		}
//		String sep = "\\"+File.separator;
//		Pattern pattern2 = Pattern.compile(sep+"$");
//		Matcher matcher2 = pattern2.matcher(path);
//		if (matcher2.find()) {
//			path = path.substring(0, path.length()-1);
//			path = removeLastSeparator(path);
//		}
		String sep = File.separator;
		if (path.endsWith(sep)) {
			path = path.substring(0, path.length()-1);
			path = removeLastLinuxSeparator(path);
		}
		return path;
	}
	
	public static String removeLastLinuxSeparator(String path){
		// 递归去掉 所有 结尾 /
		if (path.length() < 1) {
			return path;
		}
		String sep = "/";
		if (path.endsWith(sep)) {
			path = path.substring(0, path.length()-1);
			path = removeLastLinuxSeparator(path);
		}
		return path;
	}
	
	/** 单文件移动 
	 * @param srcPath 源文件
	 * @param dstPath 目标文件
	 * @return
	 */
	public static boolean singleFileMove(String srcPath, String dstPath){
		boolean isSuccess = false;
		File dstfile = new File(dstPath);
		dstfile.delete();
		File srcfile = new File(srcPath);
		if(srcfile.exists()){
			// windows linux下通用的 迁移数据
			isSuccess = srcfile.renameTo(dstfile);
		}
		return isSuccess;
	}
}
