将表中得内容写到文件中

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;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值