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位数分开分别产生,方法很多
如有说错之处,请指出,相互学习,嘿嘿
java 操作配置文件 .properties的更多相关文章
- 操作配置文件Properties
// */ // ]]> 操作配置文件Properties Table of Contents 1 定义 2 读取配置值 3 修改和保存配置 4 注意 1 定义 csharp中在Settin ...
- java对配置文件properties的操作
1.读取配置文件的键值对,转为Properties对象:将Properties(键值对)对象写入到指定文件. package com.ricoh.rapp.ezcx.admintoolweb.util ...
- Java读写配置文件——Properties类的简要使用笔记
任何编程语言都有自己的读写配置文件的方法和格式,Java也不例外. 在Java编程语言中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XM ...
- volicity 模板类,java操作配置文件
import java.io.StringWriter; import java.util.HashSet; import java.util.Iterator; import java.util.M ...
- java读取配置文件(properties)的时候,unicode码转utf-8
有时我们在读取properties结尾的配置文件的时候,如果配置文件中有中文,那么我们读取到的是unicode码的中文,需要我们在转换一下,代码如下 /** * 将配置文件中的Unicode 转 ut ...
- Java 读取配置文件 Properties
String filePath="src/cn/ac/iscas/pebble/ufe/conf/id.properties"; InputStream in = new Buff ...
- Java中Properties类的操作配置文件
知识学而不用,就等于没用,到真正用到的时 候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用 Java来写, ...
- Java配置文件Properties的读取、写入与更新操作
/** * 实现对Java配置文件Properties的读取.写入与更新操作 */ package test; import java.io.BufferedInputStream; import j ...
- 对Java配置文件Properties的读取、写入与更新操作
http://breezylee.iteye.com/blog/1340868 对Java配置文件Properties的读取.写入与更新操作 博客分类: javase properties 对Jav ...
随机推荐
- spring+hibernate
添加注解时spring是添加在set方法上而hibernate是添加在get方法上 事务应该加在service层上,而不是加在dao层上,如果加在dao层上回滚的时候只能回滚一个dao的实现. 事务处 ...
- JS基础如何理解对象
这几天跟几个同事聊天发现他们对javascript什么时候该用new都不是很了解. 1.javascript的function什么时候该new什么时候不该new?我觉得主要的问题还是集中在javasc ...
- [转]用Gson来解析Json数据
转自太阳尚远的博客:http://blog.yeqianfeng.me/2016/03/02/use_gson_to_parse_json/ 在我们实际开发中像Json和XML这两种格式的数据是最常见 ...
- 关于 java.io.IOException: open failed: EACCES (Permission denied)
今天解决了一个问题,不得不来和大家分享.就是关于 java.io.IOException: open failed: EACCES (Permission denied)的问题,网上也有很多人把这个问 ...
- c# xml操作类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xm ...
- ajax跨域请求的方案
$.get("@Hosts.Default.Www/api/XXXXX/Getxxx/"+@Model.UserId, function(data) { $("#tota ...
- Nginx 介绍和安装
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- SEO人员应该突破的5大思想误区
1.外链误区 很多人在做网站优化的时候容易陷入外链怪圈,认为外链就是网站优化的一切,只要做好外链排名就会上去,让他不做外链了他将不知道做什么.特别是外链专员和一些初级SEO人员,优化网站只知道到哪里发 ...
- hdu 5583 Kingdom of Black and White(模拟,技巧)
Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black fro ...
- CodeSmith exclude global 文件和文件夹问题 与 输入中文显示乱码问题
1.打开C:/Documents and Settings/你的用户名/Application Data/CodeSmith/v4.1/CodeSmithGui.config文件. 2.在<te ...