平平庸庸

java notes

上一篇 / 下一篇  2009-05-12 09:55:21 / 个人分类:测试工具

为不浪费你的宝贵时间请勿阅读此篇日志

//netbean 发布程序

jar cvmf MANIFEST tools.jar tool

//获得当前项目路径
System.out.println(System.getProperty("user.dir"));

//java 配置文件读取

package configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/** *//**
 * 读取properties文件
 * @author Qutr
 *
 */
public class Configuration
...{
    private Properties propertie;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;
   
    /** *//**
     * 初始化Configuration类
     */
    public Configuration()
    ...{
        propertie = new Properties();
    }
   
    /** *//**
     * 初始化Configuration类
     * @param filePath 要读取的配置文件的路径+名称
     */
    public Configuration(String filePath)
    ...{
        propertie = new Properties();
        try ...{
            inputFile = new FileInputStream(filePath);
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) ...{
            System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
            ex.printStackTrace();
        } catch (IOException ex) ...{
            System.out.println("装载文件--->失败!");
            ex.printStackTrace();
        }
    }//end ReadConfigInfo(...)
   
    /** *//**
     * 重载函数,得到key的值
     * @param key 取得其值的键
     * @return key的值
     */
    public String getValue(String key)
    ...{
        if(propertie.containsKey(key))...{
            String value = propertie.getProperty(key);//得到某一属性的值
            return value;
        }
        else
            return "";
    }//end getValue(...)
   
    /** *//**
     * 重载函数,得到key的值
     * @param fileName properties文件的路径+文件名
     * @param key 取得其值的键
     * @return key的值
     */
    public String getValue(String fileName, String key)
    ...{
        try ...{
            String value = "";
            inputFile = new FileInputStream(fileName);
            propertie.load(inputFile);
            inputFile.close();
            if(propertie.containsKey(key))...{
                value = propertie.getProperty(key);
                return value;
            }else
                return value;
        } catch (FileNotFoundException e) ...{
            e.printStackTrace();
            return "";
        } catch (IOException e) ...{
            e.printStackTrace();
            return "";
        } catch (Exception ex) ...{
            ex.printStackTrace();
            return "";
        }
    }//end getValue(...)
   
    /** *//**
     * 清除properties文件中所有的key和其值
     */
    public void clear()
    ...{
        propertie.clear();
    }//end clear();
   
    /** *//**
     * 改变或添加一个key的值,当key存在于properties文件中时该key的值被value所代替,
     * 当key不存在时,该key的值是value
     * @param key 要存入的键
     * @param value 要存入的值
     */
    public void setValue(String key, String value)
    ...{
        propertie.setProperty(key, value);
    }//end setValue(...)
   
    /** *//**
     * 将更改后的文件数据存入指定的文件中,该文件可以事先不存在。
     * @param fileName 文件路径+文件名称
     * @param description 对该文件的描述
     */
    public void saveFile(String fileName, String description)
    ...{
        try ...{
            utputFile = new FileOutputStream(fileName);
            propertie.store(outputFile, description);
            outputFile.close();
        } catch (FileNotFoundException e) ...{
            e.printStackTrace();
        } catch (IOException ioe)...{
            ioe.printStackTrace();
        }
    }//end saveFile(...)
   
    public static void main(String[] args)
    ...{
        Configuration rc = new Configuration(".\config\test.properties");//相对路径
       
        String ip = rc.getValue("ipp");//以下读取properties文件的值
        String host = rc.getValue("host");
        String tab = rc.getValue("tab");
       
        System.out.println("ip = " + ip + "ip-test leng = " + "ip-test".length());//以下输出properties读出的值
        System.out.println("ip's length = " + ip.length());
        System.out.println("host = " + host);
        System.out.println("tab = " + tab);

        Configuration cf = new Configuration();
        String ipp = cf.getValue(".\config\test.properties", "ip");
        System.out.println("ipp = " + ipp);
//        cf.clear();
        cf.setValue("min", "999");
        cf.setValue("max", "1000");
        cf.saveFile(".\config\save.perperties", "test");
       
//        Configuration saveCf = new Configuration();
//        saveCf.setValue("min", "10");
//        saveCf.setValue("max", "1000");
//        saveCf.saveFile(".\config\save.perperties");
       
    }//end main()
   
}//end class ReadConfigInfo

//一个比较简单的读取配置文件的例子

public class Test1 {

public String getPara(String fileName) {
Properties prop= new Properties();
try {
InputStream is = getClass().getResourceAsStream(fileName);
prop.load(is);
if(is!=null) is.close();
}
catch(Exception e) {
System.out.println(e+"file "+fileName+" not found");
}
return prop.getProperty("file_name");
}
public static void main(String[] args) {
Test1 test = new Test1();
System.out.println(test.getPara("db.property"));
}
}

//    Enumeration 枚举
     Enumeration en=v.elements();
        int sum=0;
        int c=0;
        while(en.hasMoreElements())
        {
           Integer b=(Integer)en.nextElement();
           c=b.intValue();
           System.out.println(c);
          }
        sum=sum+c;
        System.out.println("他们的和为:"+sum);
    }
}

//
     String[] strings=new String[10];
     //读取配置文件信息

     private String[] getlist(){
        Properties prop= new Properties();
        try {
            InputStream is =getClass().getResourceAsStream(" ");
            prop.load(is);
            if(is!=null) is.close();
        }
        catch(Exception e) {
            System.out.println(e+"config file not found");
        }
        prop.propertyNames();
        Enumeration keys=prop.propertyNames();
        int sum=0;
        int count=0;
        while(keys.hasMoreElements()){
            strings[count]=(String) keys.nextElement();
            sum++;
            count++;
        }

        return strings;
     }
//写行


try {
        BufferedWriter ut = new BufferedWriter(new FileWriter("filename", true));
        out.write("aString");
 
       // 这里继续写其它的。。。。
 
        out.close();
    } catch (IOException e) {
    }
//log4j

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testsample;
import org.apache.log4j.*;
/**
 *
 * @author peter.sun
 */
public class add {
   
    static  Logger logger1=Logger.getLogger(add.class);
   
    public int add(int a,int b){
       
        int sum=a+b;
        logger1.info("sum is "+Integer.toString(sum));
        return sum;
      

    }

 

}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testsample;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.log4j.*;

/**
 *
 * @author peter.sun
 */
public class addTest {

    public addTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
        System.out.println(System.getProperty("user.dir"));
        PropertyConfigurator.configure("log4j.properties");
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of add method, of class add.
     */
    @Test
    public void testAdd() {
        System.out.println("add");
        int a = 1;
        int b = 2;
        add instance = new add();
        int expResult = 3;
        int result = instance.add(a, b);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        //fail("The test case is a prototype.");
    }

}
//oracle connection
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package LrOralce;

/**
 *
 * @author peter.sun
 */
import java.sql.*;
import java.util.Date;
import java.util.Properties;

public class LrOracleClient {
String sDbDriver = "oracle.jdbc.driver.OracleDriver";
String sConnStr = "jdbc:*:*:ip_adress:port/sid";
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
public LrOracleClient()
{
try {
Class.forName(sDbDriver);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("testdb():" + e.getMessage());
}
}
public void executeUpdate()
{
stmt = null;
rs = null;
try {
conn = DriverManager.getConnection(sConnStr, "*",
"*");
stmt = conn.createStatement();
System.out.println(new Date());
int j=0;
for(int i=0;i<10000;i++){
j=10001+i;
//追加
String sql="insert into tablename(*, *, *...)"+"values('test"+i+"',*,*...)";
//更新
//sql="update tablename set
//field_no='testupdate',*,*... where filed_no="+j;
//删除
//sql="delete from tablename where
//tablename="+j;
//查询
//sql="select * from tablename where
//tablename="+j;
stmt.executeUpdate(sql);
//rs=stmt.executeQuery(sql);
//rs.next();
}
System.out.println(new Date());
//rs.close();
stmt.close();
conn.close();
System.out.println("Update is secuessful!");
} catch (SQLException ex) {
System.err.println("executeUpdate:" +
ex.getMessage());
}
}
public void executeSql(String sql)
{
stmt = null;
rs = null;
try {
conn = DriverManager.getConnection(sConnStr, "*", "*");
stmt = conn.createStatement();
rs=stmt.executeQuery(sql);
rs.next();
System.out.println("Count is "+rs.getString(1));
stmt.close();
conn.close();
//System.out.println("OK");
} catch (SQLException ex) {
System.err.println("executeSql:" + ex.getMessage());
}
}
/*
public static void main(String[] args) {
testsybase mytest = new testsybase();
mytest.executeUpdate();
}
 * */
}


TAG:

 

评分:0

我来说两句

Open Toolbar