FileUtils.java 7.12 KB
package com.sitech.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sitech.ismp.messageObject.AlarmEvent;
import com.sitech.ismp.messageObject.PerformanceObject;

public class FileUtils {
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * �����
	 * 
	 * @param file
	 * @param file2
	 * @return
	 */
	public static boolean reName(File file, File file2) {

		if (!file.exists()) {
			return false;
		}
		return file.renameTo(file2);
	}

	/**
	 * �ж��ļ��Ǵ���
	 * 
	 * @param path
	 * @param filename
	 * @return
	 */
	public boolean isExists(String path, String filename) {

		File file = new File(path, filename);
		if (!file.exists()) {
			return false;
		}
		return true;
	}

	/**
	 * �õ�pathĿ¼�������ļ�
	 * 
	 * @param path
	 * @return
	 */
	public synchronized static File[] getDirFiles(String path) {
		File file = new File(path);
		if (!file.exists())
			return null;
		return file.listFiles();
	}

	public boolean delFile(String path, String filename) {
		File file = new File(path, filename);
		if (!file.exists()) {
			return false;
		}
		return file.delete();
	}

	public int readFileLine(String path, String filename) {
		int i = 0;
		File file = new File(path, filename);

		if (!file.exists()) {
			System.out.println(path + filename + " ������!");
			return 0;
		}

		FileInputStream fis = null;
		BufferedReader bfd = null;

		try {
			fis = new FileInputStream(file);
			bfd = new BufferedReader(new InputStreamReader(fis, "GBK"));
			String line = "";
			while ((line = bfd.readLine()) != null) {
				String[] kpis = line.split("\t");
				System.out.println(kpis[0]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bfd != null) {
					bfd.close();
				}
				if (fis != null) {
					fis.close();
				}				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return i;
	}

	public static boolean writeFile(String path, String filename,
			String content, boolean append) {

        File dir = new File(path);
        if(!dir.exists()){
            dir.mkdir();
        }

		BufferedWriter out = null;
		FileOutputStream fos = null;
		filename = filename.trim();
		path = path.trim();
		File file = new File(path, filename);

		try {
			if (file.exists() && append) {
				content = "\r\n" + content;
			}
			fos = new FileOutputStream(file, append);
			out = new BufferedWriter(new OutputStreamWriter(fos, "GBK"));
			out.write(content);
			out.flush();
		} catch (Exception e) {
            throw new RuntimeException("生成脚本文件失败!", e);
		} finally {
			try {
				out.close();
				fos.close();
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}

	/**
	 * �õ������
	 * 
	 * @param path
	 * @param filename
	 * @return
	 */
	public FileUtilsBean openBuffer(String path, String filename) {
		FileUtilsBean bean = new FileUtilsBean();
		BufferedWriter out = null;
		FileOutputStream fos = null;
		filename = filename.trim();
		path = path.trim();
		File file = new File(path, filename);
		try {
			fos = new FileOutputStream(file, true);
			out = new BufferedWriter(new OutputStreamWriter(fos, "GBK"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		bean.setFile(file);
		bean.setFos(fos);
		bean.setOut(out);
		return bean;
	}

	public void writeContext(BufferedWriter writer, String context) {
		try {
			writer.write(context + "\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * �ļ��ƶ�
	 * 
	 * @param sourcePath
	 * @param newPath
     * @param oldfileName
     * @param newfileName
	 */
	public boolean moveFile(String sourcePath, String newPath,
			String oldfileName, String newfileName) {
		boolean result = true;
		File oldfile = new File(sourcePath, oldfileName);
		File newfile = new File(newPath, newfileName);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(oldfile);
			fos = new FileOutputStream(newfile);
			byte[] byte_in = new byte[fis.available()];

			fis.read(byte_in);
			fos.write(byte_in);
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
				if (fos != null) {
					fos.close();
				}	
			} catch (IOException e1) {
				result = false;
				e1.printStackTrace();
			}
		}
		return result;
	}

	public static boolean saveFileToHarddisk(String realfolderpath,
			String filename, byte[] context) {
		try {
			File dir = new File(realfolderpath);
			if (!dir.exists())
				dir.mkdirs();
			File file = new File(dir, filename);
			FileOutputStream out = new FileOutputStream(file);
			out.write(context);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * �ļ��ƶ�
	 * 
	 * @param tmp_path
	 * @param filename
	 * @param data_path
	 * @return
	 */
	public boolean moveFile(String tmp_path, String filename, String data_path) {
		// String cmd = "cmd /c mv /y " + tmp_path + "\\" + filename + " " +
		// data_path;
		boolean result = true;
		// if(Config.plat_type.equals("unix"))
		// cmd = "mv " + tmp_path + "/" + filename + " " + data_path;
		// if(Config.plat_type.equals("hp_unix"))
		// cmd = "mv " + tmp_path + "/" + filename + " " + data_path;
		// try {
		// Sheller.executeCmd(cmd);
		// } catch (Exception e) {
		// result = false;
		// }
		return result;
	}

	public synchronized static void writeFailureFile(String path, Object obj) {
		String pre = "";
		String context = "";
		if (obj instanceof PerformanceObject) {
			PerformanceObject kpidetail = (PerformanceObject) obj;
			pre = "KPI_" + kpidetail.getUnitId() + "_" + kpidetail.getKpiId()
					+ "_";
			context = getKpidetailContext(obj);
		} else if (obj instanceof AlarmEvent) {
			AlarmEvent event = (AlarmEvent) obj;
			pre = "ALARM_" + event.getUnitId() + "_" + event.getKpiId() + "_";
			context = getAlarmContext(obj);
		} else {
			return;
		}

		long timestamp = new Date().getTime();
		String fileName = pre + timestamp;
		writeFile(path, fileName, context, true);
	}

	private static String getAlarmContext(Object obj) {

		return null;
	}

	private static String getKpidetailContext(Object obj) {

		return null;
	}

	public static PerformanceObject getPerformanceObject(File file) {

		return null;
	}

	public static AlarmEvent getAlarmEvent(File file) {
		return null;
	}

    public static boolean createNewFile(String pathName, String fileName) {
        File path = new File(pathName);
        if (!path.exists()) {
            path.mkdirs();
        }

        File file = new File(pathName, fileName);

        try {
            if(!file.createNewFile()){
                throw new Exception("Exception while create new file:" + pathName + File.separator + fileName);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return true;
    }
}