• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

java 操作配置文件 .properties

package com.dms.common;

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

public class GetDayRandomNumber {

 private  Properties prop = new Properties();
 private final  String path = "pageparam.properties";
 private  String patha = "";
 
 public GetDayRandomNumber(){
  this.getFilePath();
  this.loadFile();
 }
 
 private  void getFilePath(){   //获取配置文件的路径
  patha = this.getClass().getClassLoader().getResource("").getPath()+"com/dms/config"+"/"+path;
 }
 
 public void loadFile(){//加载文件
  FileInputStream in = null;
  try {
   
   File file = new File(patha);
   in = new FileInputStream(file);
   prop.load(in);
   
  } catch (IOException e) {
   e.printStackTrace();
   throw new ExceptionInInitializerError();
  } finally {
   try {
    in.close();
   } catch (Exception e) {
   }
  }
 }
 
 public  void modifyValue(String key , String value){//修改文件
  File file = new File(patha);
  prop.setProperty(key, value);
  FileOutputStream out;
  try {
   out = new FileOutputStream(file);
   prop.store(out, "");//写入的格式
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 
 public  String getValue(String key) {   //直接通过主键获取对应的内容
  return prop.getProperty(key);
 }
 }

1、主要注意的是 如果在开发平台运行 如



 

则工程里面的文件是没有内容 如



 

 

2、要看见内容,需要再打包后的工程里面找 如



 

 

3、顺便说下随机数的产生方法

3.1、问题:产生一个3位数的随机数(100--999)

3.2、思路:因为随机函数Random只能指定上限,却不能指定一个范围;

                  所以需要借用循环体来实现;

                  当满足条件时,跳出;不满足条件时,接着执行;

3.3、实现方法:

Random rd = new Random();
   int random = 0;
    do{
     random = rd.nextInt(999);
    }while(random<100);

也可以将3位数分开分别产生,方法很多

 

 

如有说错之处,请指出,相互学习,嘿嘿

 

posted @ 2013-08-29 19:57  Class Xman  阅读(224)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3