JAVA开发6--java项目实现读取配置文件相关

1.配置文件 VAR.PROPERTIES 内容:

YEAR:2016
MONTH:07
DATA_TYPE:OA_ProjectBudget


2.将VAR2.PROPERTIES 文件放在项目文件夹下:

在项目文件夹下新建 config 文件夹:

比如:

D:\documents\Workspace\Myeclipse\OAInterface

下新建 文件夹 config。

然后将 VAR.PROPERTIES 文件 放在config文件夹下。

(作用:开发代码时,通过读取当前目录,实现读取相对路径下的文件)


3.开发--部分代码:


//        3. 从配置文件中读取 month 和 year
//        获取当前目录
        String cur_dir = System.getProperty("user.dir");//获取当前目录
        String new_dir = cur_dir.replace("\\","\\\\");//将斜杠进行替换
        Properties prop = new Properties();
//        InputStream in = new BufferedInputStream (new FileInputStream("F:\\working\\08.公牛\\04.OA接口开发\\01.JAVA开发\\OAInterface\\config\\var.properties"));
//        InputStream in = new BufferedInputStream (new FileInputStream("D:\\OAInterface\\config\\var.properties"));
        InputStream in = new BufferedInputStream (new FileInputStream(new_dir + "\\config\\var2.properties"));
        prop.load(in);     ///加载属性列表
        Iterator<String> it=prop.stringPropertyNames().iterator();
        String data_type_key = it.next();
        String data_type = prop.getProperty(data_type_key);
        String month_key = it.next();
        String month_value = prop.getProperty(month_key);
        String year_key = it.next();
        String year_value = prop.getProperty(year_key);
        in.close();
        int year = Integer.parseInt(year_value);
        int month = Integer.parseInt(month_value);


此处实现的是,读取同目录下 config文件夹 下的  VAR.PROPERTIES 文件。


4.完整代码粘贴:

package com.noahark.dao;

/**
* @author guoky
* @version 创建时间:2016-10-13
* 调用webservice取数--费用预算
*/

import gn.budget.service.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;
//import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

public class ImportData {

//    @SuppressWarnings("unchecked") 屏蔽警告信息;
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException_Exception, DocumentException, SQLException, IOException{
    
//        获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String start_time = df.format(new Date());
        System.out.println("\n************************start_time : " + start_time + "************************");// new Date()为获取当前系统时间

        /**
        //1. 定义两个变量 年 和 月 可以取当前年和月
        SimpleDateFormat df_year = new SimpleDateFormat("yyyy");//设置日期格式
        SimpleDateFormat df_month = new SimpleDateFormat("MM");//设置日期格式
        String year = df_year.format(new Date()) ;//转换为String
        String month = df_month.format(new Date()) ;//转换为String
        */
        
        /**
        //2. 定义两个变量 年 和 月 接受控制台输入
        Scanner sc = new Scanner(System.in);
        String year = sc.nextLine();
        String month = sc.nextLine();
        */
    
//        3. 从配置文件中读取 month 和 year
//        获取当前目录
        String cur_dir = System.getProperty("user.dir");//获取当前目录
        String new_dir = cur_dir.replace("\\","\\\\");//将斜杠进行替换
        Properties prop = new Properties();
//        InputStream in = new BufferedInputStream (new FileInputStream("F:\\working\\08.公牛\\04.OA接口开发\\01.JAVA开发\\OAInterface\\config\\var.properties"));
//        InputStream in = new BufferedInputStream (new FileInputStream("D:\\OAInterface\\config\\var.properties"));
        InputStream in = new BufferedInputStream (new FileInputStream(new_dir + "\\config\\var.properties"));
        prop.load(in);     ///加载属性列表
        Iterator<String> it=prop.stringPropertyNames().iterator();
        String data_type_key = it.next();
        String data_type = prop.getProperty(data_type_key);
        String month_key = it.next();
        String month = prop.getProperty(month_key);
        String year_key = it.next();
        String year = prop.getProperty(year_key);
        in.close();
        
//        先 delete 中间表
        String sql_delete = "DELETE OA_TEST WHERE YEARS = '" + year + "' and period = '" + month + "' and DATA_TYPE = '" + data_type + "'";
        DBUtil jdbc = new DBUtil();
        Connection conn = jdbc.getConnection();
        Statement st = conn.createStatement();
        st.executeQuery(sql_delete);
        st.close();
        conn.close();
            
//        客户提供 ServiceBudget
        ServiceBudget Budget = new ServiceBudgetService().getServiceBudgetPort();
        
//        客户提供getBudgetMonthOccurrence方法
        String result = Budget.getBudgetMonthOccurrence(year, month);
            
//        测试输出result
//        System.out.println("输出webservice结果...\n"+result);
            
//        解析 result
        Document document =DocumentHelper.parseText(result);
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();        
        int count = 0;
        for (Element child : childElements) {
               
//            将数据插入到中间表
            String sql_insert = "insert into OA_TEST(ENTITY,ACCOUNT,YEARS,PERIOD,PRODUCT,CYCLE,SCENARIO,VERSION,PROJECT,CUSTOM1,CUSTOM2,CUSTOM3,REVERSE1,REVERSE2,REVERSE3,DATA,DATA_TYPE,CREATED) values (" + "'" + child.elementText("ysdycode") + "'" + "," + "'" + child.elementText("kmcode") + "'" + "," + "'" + child.elementText("year") + "'" + "," + "'" + child.elementText("month") + "'" + ",'PRODUCT'" + ",'CYCLE'" + ",'SCENARIO'" + ",'VERSION'" + ",'PROJECT'" + ",'CUSTOM1'" + ",'CUSTOM2'" + ",'CUSTOM3','REVERSE1','REVERSE2','REVERSE3'," +  child.elementText("je") + ",'" + data_type + "'," + "sysdate" + ")" + "";
//            String sql2 = "insert into OA_TEST values ('a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0,sysdate) ";
            DBUtil jdbc2 = new DBUtil();
            Connection conn2 = jdbc2.getConnection();
            Statement st2 = conn2.createStatement();
            st2.executeQuery(sql_insert);
            st2.close();
            conn2.close();
            count = count + 1;
        }
        String end_time = df.format(new Date());
        System.out.println("\nSucceed!" +
                "\nRECORDS : " + count +
                ";\nYEAR : " + year +
                ";\nMONTH : " + month +
                ";\n\n*************************end_time : " + end_time + "*************************");//record
    }
}

5.用bat调用jar包的相关修改:

在jar包同目录下建一个文件夹 config,将VAR.PROPERTIES 文件 拷贝在 config文件夹下。



posted @ 2016-10-18 17:50  Stepheng  阅读(71)  评论(0)    收藏  举报