package util;

 
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
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.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
 
 
public class FileUtils {
 
 public static void createPath(String filePath) {
  filePath = filePath.toString();// 中文转换
  java.io.File myFilePath = new java.io.File(filePath);
  if (!myFilePath.exists()) myFilePath.mkdir();
 }
 
 public static void createFile(String filePath) throws FileUtilException {
  try {
   filePath = filePath.toString();
   java.io.File myFilePath = new java.io.File(filePath);
   if (!myFilePath.exists()) myFilePath.createNewFile();
  } catch (IOException e) {
   e.printStackTrace();
   throw new FileUtilException("创建文件错误!");
  }
 }
 
 public static void deleteFile(String filePath) throws FileUtilException {
  try {
   filePath = filePath.toString();
   java.io.File myDelFile = new java.io.File(filePath);
   if (myDelFile.exists())
    myDelFile.delete();
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("删除文件错误!");
  }
 }
 
 public static void copyFile(String sourceFilePath, String distFilePath) throws FileUtilException {
  try {
   int bytesum = 0;
   int byteread = 0;
   InputStream inStream = new FileInputStream(sourceFilePath);
   FileOutputStream fs = new FileOutputStream(distFilePath);
   byte[] buffer = new byte[1444];
   while ((byteread = inStream.read(buffer)) != -1) {
    bytesum += byteread;
    fs.write(buffer, 0, byteread);
   }
   inStream.close();
   fs.close();
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("文件拷贝错误!");
  }
 }
 
 public static void copyFilePath(String sourceFilePath, String distFilePath) throws FileUtilException {
  try {
   (new File(distFilePath)).mkdirs();
   File[] file = (new File(sourceFilePath)).listFiles();
   for (int i = 0; i < file.length; i++) {
    if (file[i].isFile()) {
     file[i].toString();
     FileInputStream input = new FileInputStream(file[i]);
     FileOutputStream output = new FileOutputStream(distFilePath
       + "/" + (file[i].getName()).toString());
     byte[] b = new byte[1024 * 5];
     int len;
     while ((len = input.read(b)) != -1) {
      output.write(b, 0, len);
     }
     output.flush();
     output.close();
     input.close();
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("文件夹拷贝错误!");
  }
 }
 
 public static InputStream fileToStream(String filePath) throws FileUtilException {
  try {
   File file = new File(filePath);
   if (file.exists())
    return new FileInputStream(file);
   return null;
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("文件转化输出流错误!");
  }
 }
 
 public static byte[] fileToByte(File file) throws FileUtilException {
  try {
   byte[] dist = null;
   if (file.exists()) {
    FileInputStream is = new FileInputStream(file);
    dist = new byte[is.available()];
    is.read(dist);
   }
   return dist;
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("文件转化字节数组错误!");
  }
 }
 
 public static byte[] fileToByte(String filePath) throws FileUtilException {
  try {
   File file = new File(filePath);
   byte[] dist = null;
   if (file.exists()) {
    FileInputStream is = new FileInputStream(file);
    dist = new byte[is.available()];
    is.read(dist);
   }
   return dist;
  } catch (Exception e) {
   e.printStackTrace();
   throw new FileUtilException("文件转化字节数组错误!");
  }
 }
 
 public static void writeFile(String filePath, String fileName, String[] args) throws IOException {
  FileWriter fw = new FileWriter(filePath + fileName);
  PrintWriter out = new PrintWriter(fw);
  for (int i = 0; i < args.length; i++) {
   out.write(args[i]);
   out.println();
   out.flush();
  }
  fw.close();
  out.close();
 }
 
 public static void writeFile(String filePath, String fileName, String args) throws IOException {
  FileWriter fw = new FileWriter(filePath + fileName);
  fw.write(args);
  fw.close();
 }
 
 
 public static void writeFile(String filePath, String args) throws IOException {
  FileWriter fw = new FileWriter(filePath);
  fw.write(args);
  fw.close();
 }
 
 public static boolean createAndDeleteFile(String filePath, String fileName) throws IOException {
  boolean result = false;
  File file = new File(filePath, fileName);
  if (file.exists()) {
   file.delete();
   result = true;
   System.out.println("文件已经删除!");
  } else {
   file.createNewFile();
   result = true;
   System.out.println("文件已经创建!");
  }
  return result;
 }
 
 public static boolean createAndDeleteFolder(String folderName, String filePath) {
  boolean result = false;
  try {
   File file = new File(filePath + folderName);
   if (file.exists()) {
    file.delete();
    System.out.println("目录已经存在,已删除!");
    result = true;
   } else {
    file.mkdir();
    System.out.println("目录不存在,已经建立!");
    result = true;
   }
  } catch (Exception ex) {
   result = false;
   System.out.println("CreateAndDeleteFolder is error:" + ex);
  }
  return result;
 }
 
 public static void readFolderByFile(String filePath) {
  File file = new File(filePath);
  File[] tempFile = file.listFiles();
  for (int i = 0; i < tempFile.length; i++) {
   if (tempFile[i].isFile()) {
    System.out.println("File : " + tempFile[i].getName());
   }
   if (tempFile[i].isDirectory()) {
    System.out.println("Directory : " + tempFile[i].getName());
   }
  }
 }
 public static File[] readFolderByFile_(String filePath) {
  File file = new File(filePath);
  File[] tempFile = file.listFiles();
   return tempFile;
 }
 
 public static boolean fileIsNull(String filePath, String fileName) throws IOException {
  boolean result = false;
  FileReader fr = new FileReader(filePath + fileName);
  if (fr.read() == -1) {
   result = true;
   System.out.println(fileName + " 文件中没有数据!");
  } else {
   System.out.println(fileName + " 文件中有数据!");
  }
  fr.close();
  return result;
 }
 
 public static void readAllFile(String filePath, String fileName) throws IOException {
  FileReader fr = new FileReader(filePath + fileName);
  int count = fr.read();
  while (count != -1) {
   count = fr.read();
   if (count == 13) {
    fr.skip(1);
   }
  }
  fr.close();
 }
 
 public static void readLineFile(String filePath, String fileName) throws IOException {
  FileReader fr = new FileReader(filePath + fileName);
  BufferedReader br = new BufferedReader(fr);
  String line = br.readLine();
  while (line != null) {
   line = br.readLine();
  }
  br.close();
  fr.close();
 }
 
 
 public static String readLineFile(String filePath) throws IOException {
  StringBuffer sb = new StringBuffer();
  FileReader fr = new FileReader(filePath);
  BufferedReader br = new BufferedReader(fr);
  String line = br.readLine();
  while (line != null) {
   sb.append(line);
   line = br.readLine();
  }
  br.close();
  fr.close();
  return sb.toString();
 }
 
 
 
 
 public String randomFileName() throws Exception {
  try {
//   return GetUID.newGUID().toString();
  } catch (Exception ee) {
   ee.printStackTrace();
   throw new Exception("产生文件名称出现错误:" + ee.getMessage());
  }
  return null;
 }
 
 public ByteArrayInputStream file2ByteArrayInputStream(String fileName)
   throws Exception {
  try {
   File file = new File(fileName);
   return file2ByteArrayInputStream(file);
  } catch (Exception e) {
   throw new Exception("将文件转换为流的过程中出现错误!");
  }
 }
 
 public ByteArrayInputStream file2ByteArrayInputStream(File file)
   throws Exception {
  try {
   FileInputStream is = new FileInputStream(file);
   byte[] b = new byte[is.available()];
   is.read(b);
   is.close();
   file.delete();
   return new ByteArrayInputStream(b);
  } catch (Exception e) {
   throw new Exception("将文件转换为流的过程中出现错误!");
  }
 }
 
 public String readFromURL(String strURL) {
  try{
   URL url = new URL(strURL);
   BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
   String str;
   String rtnStr="";
   while((str=in.readLine())!=null){
    rtnStr = rtnStr+new String(str.getBytes(), "GB2312");
   }
   in.close();
   return rtnStr;
  }catch(MalformedURLException e){
   e.printStackTrace();
   return null;
  }catch(IOException e){
   e.printStackTrace();
   return null;
  }catch(Exception e){
   e.printStackTrace();
   return null;
  }  
 }
 
 public String readFromIS(InputStream is) throws Exception{
  try {   
   String strRtn = "";
   int length = is.available();
   byte[] buf = new byte[length];
   int num = 0;
   while ((num = is.read(buf, 0, length)) != -1) {
    strRtn = strRtn + new String(buf, 0, length, "GB2312");
   }   
   return strRtn;
  } catch (IOException e) {
   e.printStackTrace();
   return null;
  }finally{
   is.close();
  }
 }
 public static String getFileName(File file) throws Exception{
  String fileName = file.getName();
  String[] str = fileName.split("\\.");
  return str[0];
 }
 public static String getFileType(File file) throws Exception{
  String fileName = file.getName();
  String[] str = fileName.split("\\.");
  if(str!=null && str.length>1){
   return str[1];
  }  
  return null;
 }
}