Properties应用的一个例子

package com.aaa.aaa;

import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  long l = System.currentTimeMillis();

  System.out.println(l);

  long l2 = System.currentTimeMillis();

  System.out.println(l2 - l);

  Properties p;
  Test t = new Test();
  t.get();

 }

 public void get() {
  Properties initProp = new Properties();
  InputStream in = null;
  try {
   System.out.println("begin");
   in = getClass().getResourceAsStream(
     "/com/xxxxxxt/xxk/conf/a.properties");
   // in = getClass().getResourceAsStream("/web.xml");
   initProp.load(in);

   Set set = initProp.keySet();
   Iterator it = set.iterator();
   String tmp;
   while (it.hasNext()) {
    tmp = (String) it.next();
    System.out.println(tmp);
   }
   System.out.println("end");
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,假设你正在编写一个Java应用程序,需要读取一个名为config.properties属性文件来配置应用程序。下面是一个示例config.properties文件: ``` #database configuration db.host=localhost db.port=3306 db.name=mydatabase db.user=myusername db.password=mypassword #application configuration app.title=MyApp app.version=1.0 app.author=John Doe ``` 在Java中,你可以使用以下代码读取这个属性文件: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class AppConfig { private static final String CONFIG_FILE = "config.properties"; private static Properties properties; static { properties = new Properties(); try { FileInputStream inputStream = new FileInputStream(CONFIG_FILE); properties.load(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public static String getDbHost() { return properties.getProperty("db.host"); } public static int getDbPort() { return Integer.parseInt(properties.getProperty("db.port")); } public static String getDbName() { return properties.getProperty("db.name"); } public static String getDbUser() { return properties.getProperty("db.user"); } public static String getDbPassword() { return properties.getProperty("db.password"); } public static String getAppTitle() { return properties.getProperty("app.title"); } public static String getAppVersion() { return properties.getProperty("app.version"); } public static String getAppAuthor() { return properties.getProperty("app.author"); } } ``` 这个示例代码中,我们定义了一个名为AppConfig的Java类,其中包含了静态方法来获取不同的属性。在类的静态块中,我们使用FileInputStream和Properties类来读取config.properties文件,并将其保存在一个静态的properties变量中。然后,我们定义了一系列静态方法来获取各种属性,例如getDbHost()方法返回db.host属性。这样,其他的Java类就可以通过调用AppConfig类中的这些静态方法来获取属性,并用这些来配置应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值