package com.saga.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.zillion.util.log.LogUtil;

public class CSVUtil {
	public static void createFile(File csvFile) {
		if (!csvFile.exists()) {
			try {
				csvFile.createNewFile();
				LogUtil.info(csvFile.getPath() + csvFile.getName() + " Created!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					Thread.sleep(10L);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static void deleteFile(File csvFile) {
		if (csvFile.exists()) {
			csvFile.delete();
			LogUtil.info(csvFile.getPath() + csvFile.getName() + " Deleted!");
			try {
				Thread.sleep(10L);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void insert(File csvFile, StringBuffer sb) {
		try {
			BufferedWriter csvWtriter = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(csvFile, true), "GB2312"), 1024);
			LogUtil.info(csvFile.getPath() + csvFile.getName() + " Insert into " + sb.toString());
			csvWtriter.write(sb.toString());
			csvWtriter.flush();
			csvWtriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
