package xiaoqu.home.open.utils;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class FileUtil {

    /**
     * java写文件
     * @param content
     * @param fileName
     * @return
     * @throws Exception
     */
    public static boolean writeTxtFile(String content,File  fileName)throws Exception{
        RandomAccessFile mm=null;
        boolean flag=false;
        FileOutputStream o=null;
        try {
            o = new FileOutputStream(fileName);
            o.write(content.getBytes("utf-8"));
            o.close();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(mm!=null){
                mm.close();
            }
        }
        return flag;
    }
    /**
	 * 读取文件
	 * 
	 * @param fileName
	 * @return
	 */
	public static String ReadFileByName(String fileName) {
		return ReadFileByName(fileName, "utf-8");
	}

	/**
	 * 读取文件到字节数组
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] ReadFileToByte(String filePath) {
		byte content[] = null;
		try {
			FileInputStream _FileInputStream = new FileInputStream(filePath);
			content = new byte[(int) _FileInputStream.getChannel().size()];
			_FileInputStream.read(content, 0, content.length);
			_FileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}

	/**
	 * 读取文件，指定字符集
	 * 
	 * @param fileName
	 * @param charset
	 * @return
	 */
	public static String ReadFileByName(String fileName, String charset) {
		try {
			if (new File(fileName).exists()) {
				return ReadFileByName(new FileInputStream(fileName), charset);
			} else {
				return "FileNameError";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "FileReadException";
		}
	}

	/**
	 * 读取文件，指定字符集
	 * 
	 * @param fileName
	 * @param charset
	 * @return
	 */
	public static ArrayList<String> ReadFileByNameToArray(String fileName, String charset) {
		try {
			if (new File(fileName).exists()) {
				return ReadFileByNameToArray(new FileInputStream(fileName), charset);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 读取数据从流，指定字符集
	 * 
	 * @param _InputStream
	 * @param charset
	 * @return
	 */
	public static String ReadFileByName(InputStream _InputStream, String charset) {
		StringBuilder _StringBuilder = new StringBuilder();
		try {
			BufferedInputStream _BufferedInputStream = new BufferedInputStream(_InputStream);
			BufferedReader _BufferedReader = new BufferedReader(new InputStreamReader(_BufferedInputStream, charset));
			String temp = null;
			while ((temp = _BufferedReader.readLine()) != null) {
				_StringBuilder.append(temp + "\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _StringBuilder.toString();
	}

	/**
	 * 读取文件以一行为分割返回以行的数组
	 * 
	 * @param _InputStream
	 * @param charset
	 * @return
	 */
	public static ArrayList<String> ReadFileByNameToArray(InputStream _InputStream, String charset) {
		ArrayList<String> dataList = new ArrayList<String>();
		try {
			BufferedInputStream _BufferedInputStream = new BufferedInputStream(_InputStream);
			BufferedReader _BufferedReader = new BufferedReader(new InputStreamReader(_BufferedInputStream, charset));
			String temp = null;
			while ((temp = _BufferedReader.readLine()) != null) {
				dataList.add(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dataList;
	}
}
