package com.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class NewFieldUtileTools {
	
	/**
	 * 生成文件
	 * @param filePath
	 * @param fileName
	 * @param fileContent
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 */
	public static boolean fileOperation(String filePath, String fileName,String fileContent){
		boolean sign = false;
		String pathTemp = filePath + "\\" + fileName;
		File file = new File(pathTemp);
		File file2 = new File(filePath);
	    file2.mkdirs();
		try {
			// 文件不存在
			if (!file.exists()) {

				sign = createFile(pathTemp);// 创建文件
				if (sign) {// 文件创建成功
					writeFile(pathTemp, fileContent);// 写入内容
				}
			} else {
				sign = deleteFile(pathTemp);// 删除文件
				sign = createFile(pathTemp);// 创建文件
				if (sign) {// 文件创建成功
					writeFile(pathTemp, fileContent);// 写入内容
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sign;
	}
	/**
	 * 创建文件
	 * 
	 * @throws IOException
	 */
	public static boolean createFile(String name) throws IOException {
		boolean flag = false;
		File filename = new File(name);
		if (!filename.exists()) {
			filename.createNewFile();
			flag = true;
		}
		return flag;
	}

	/**
	 * 写文件
	 * 
	 * @param newStr
	 *            新内容
	 * @throws IOException
	 */
	public static boolean writeFile(String filePath, String newStr)
			throws IOException {
		// 先读取原有文件内容，然后进行写入操作
		boolean flag = false;
		String filein = newStr + "\r\n";
		// String str = new String(filein.getBytes("UTF-8"),"UTF-8");
		String temp = "";
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		FileOutputStream fos = null;
		// PrintWriter pw = null;
		Writer pw = null;
		try {
			File file = new File(filePath);
			// 将文件读入输入流
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, "utf-8");
			br = new BufferedReader(isr);
			StringBuffer buf = new StringBuffer();
			// // 文件路径
			// File file = new File(filePath);
			// // 将文件读入输入流
			//		  
			// isr = new InputStreamReader(new FileInputStream(file),"utf-8");
			// br = new BufferedReader(isr);
			// StringBuffer buf = new StringBuffer();
			/**
			 * File f=new File(rawfilename); if(f.exists()) { BufferedReader
			 * br=null; try { String line; InputStreamReader read = new
			 * InputStreamReader(new FileInputStream(f),"utf-8"); br=new
			 * BufferedReader(read);
			 */
			// 保存该文件原有的内容
			for (int i = 1; (temp = br.readLine()) != null; i++) {
				buf = buf.append(temp);
				buf = buf.append(System.getProperty("line.separator"));
			}
			buf.append(filein);
			fos = new FileOutputStream(file);
			pw = new OutputStreamWriter(fos, "utf-8");
			pw.write(buf.toString());
			pw.flush();
			flag = true;
		} catch (IOException e1) {
			throw e1;
		} finally {
			if (pw != null) {
				pw.close();
			}
			if (fos != null) {
				fos.close();
			}
			if (br != null) {
				br.close();
			}
			if (isr != null) {
				isr.close();
			}
			if (fis != null) {
				fis.close();
			}
		}
		return flag;
	}

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