package com.iss.itreasury.system.logger.download;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.*;
public class WriteDownFile {
private String str_file_path;
private String str_file_name = "Export";
private String str_fields_object;
private String str_method_object;
private String getFilePath()
{
String str_application_path = "";
File directroy = new File(str_application_path);
try {
str_application_path = directroy.getCanonicalPath();
} catch (IOException ex) {
}
return str_application_path.length()>0?str_application_path:"";
}
public boolean writeFile (Collection result) throws Exception
{
String strDate = "";
boolean is_write_success = false;
str_file_path = getFilePath();
strDate = getCurrectDate();
if(str_file_path==null && str_file_path.length()<=0)
return is_write_success;
else
str_file_path += "//download//";
if(str_file_name==null && str_file_name.length()<=0)
return is_write_success;
else
str_file_name += strDate + ".txt";
if(result.isEmpty() && result.size()<=0)
return is_write_success;
try{
File m_file = new File(str_file_path);
if (!m_file.exists())
{
m_file.mkdirs();
}
File m_old_file = new File(str_file_path + str_file_name);
if (m_old_file.exists())
{
m_old_file.delete();
}
FileWriter m_file_write = new FileWriter(str_file_path + str_file_name,true);
String str_file_line = "";
if(str_fields_object==null && str_fields_object.length()<=0)
return is_write_success;
Object object = getObject(str_fields_object);
//写文件标题
Vector filed_object = (Vector)getAllFields(object);
if(!filed_object.isEmpty() && filed_object.size()>0)
{
for(int i=0; i<filed_object.size(); i++)
{
str_file_line += (String)filed_object.get(i) + "/t";
}
str_file_line += "/r/n";
}
m_file_write.write(str_file_line);
m_file_write.flush();
str_file_line = "";
Vector method_object = (Vector)result;
for(int i=0; i<method_object.size(); i++)
{
Object obj = method_object.get(i);
Vector method_values = (Vector)copy(obj);
for(int j=0; j<method_values.size(); j++)
{
str_file_line += (String)method_values.get(j) + "/t";
}
//写文文件
try {
m_file_write.write(str_file_line+"/r/n");
m_file_write.flush();
str_file_line = "";
}
catch (FileNotFoundException e)
{
is_write_success = false;
System.out.println(e.getMessage());
}
catch (IOException e)
{
is_write_success = false;
System.out.println(e.getMessage());
}
}
try{
m_file_write.flush();
m_file_write.close();
}
catch (FileNotFoundException e)
{
is_write_success = false;
System.out.println(e.getMessage());
}
catch (IOException e)
{
is_write_success = false;
System.out.println(e.getMessage());
}
is_write_success = true;
}
catch (Exception e)
{
e.printStackTrace();
is_write_success = false;
}
return is_write_success;
}
public Collection getAllFields(Object obj)
{
Vector v_fields = new Vector();
if(str_fields_object != null && str_fields_object.length() > 0)
{
try
{
//获得对象的类型
Class classType=obj.getClass();
//Class classType = Class.forName(str_fields_object);
//获得对象的所有属性
Field fields[]=classType.getDeclaredFields();
for(int i=0; i<fields.length; i++)
{
Field field=fields[i];
String fieldName=field.getName();
Object value=null;
try {
value=field.get(null);
} catch (IllegalAccessException ex1) {
} catch (IllegalArgumentException ex1) {
}
v_fields.add(value);
}
}catch(Exception e)
{}
}
return (v_fields.size()>0)?v_fields:null;
}
public Collection copy(Object object) throws Exception{
Vector result = new Vector();
//获得对象的类型
Class classType=object.getClass();
//通过默认构造方法创建一个新的对象
Object objectCopy=classType.getConstructor(new Class[]{}).newInstance(new Object[]{});
//获得对象的所有属性
Field fields[]=classType.getDeclaredFields();
for(int i=0; i<fields.length;i++)
{
Field field=fields[i];
String fieldName=field.getName();
String firstLetter=fieldName.substring(0,1).toUpperCase();
//获得和属性对应的getXXX()方法的名字
String getMethodName="get"+firstLetter+fieldName.substring(1);
//获得和属性对应的setXXX()方法的名字
String setMethodName="set"+firstLetter+fieldName.substring(1);
//获得和属性对应的getXXX()方法
Method getMethod=classType.getMethod(getMethodName,new Class[]{});
//获得和属性对应的setXXX()方法
Method setMethod=classType.getMethod(setMethodName,new Class[]{field.getType()});
//调用原对象的getXXX()方法
Object value=getMethod.invoke(object,new Object[]{});
//将属性值添加到集合
result.add(value);
//调用复制对象的setXXX()方法
setMethod.invoke(objectCopy,new Object[]{value});
}
return (result.size()>0?result:null);
}
public Object getObject(String className) throws Exception{
//获得对象的类型
Class classType = Class.forName(className);
//通过默认构造方法创建一个新的对象
Object objectCopy = classType.getConstructor(new Class[] {}).
newInstance(new Object[] {});
return objectCopy;
}
private void initFields()
{
if(str_file_path.equalsIgnoreCase("") && str_file_path.length()<=0)
str_file_path="C://download//";
if(str_file_name.equalsIgnoreCase("") && str_file_name.length()<=0)
str_file_name="defult";
}
public String getCurrectDate()
{
String strDate = "";
Calendar cd = Calendar.getInstance();
int intYear = cd.get(Calendar.YEAR);
int intMonth = cd.get(Calendar.MONTH) + 1;
int intDay = cd.get(Calendar.DATE);
strDate = String.valueOf(intYear) + String.valueOf(intMonth) + String.valueOf(intDay);
return strDate;
}
/*javaBean*/
public String getInitFilePath()
{
return str_file_path;
}
public String getInitFileName()
{
return str_file_name;
}
public void setFieldsObject(String fieldsObject)
{
this.str_fields_object=fieldsObject;
}
public String getFieldsObject()
{
return str_fields_object;
}
public void setMethodObject(String methodObject)
{
this.str_method_object=methodObject;
}
public String getMethodObject()
{
return str_method_object;
}
}