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 ...
随机推荐
- "创业"半年
作为一个程序员, 因为受够了”给别人实现梦想太累”的念头, 又受到”外面给出更高薪水”的诱惑, 果断离职创业. 但原本是要创业的, 过了半年, 变成了失业, 这其中到底经历了哪 ...
- javascript - 工作笔记 (事件三)
有关定义就不多说了,事件分两种 一,冒泡型事件 这是IE浏览器中事件模型的实现,顾名思义,就像水中的泡一样,自底而上,其经过的父元素都会触发对应的事件. 即:触发元素的父元素先于触发元素触发,看dem ...
- 弗洛伊德(Floyd)算法
#include <stdio.h> #define MAXVEX 20 //最大顶点数 #define INFINITY 65535 //∞ typedef struct {/* 图结构 ...
- C4.5较ID3的改进
1.ID3选择最大化Information Gain的属性进行划分 C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
Set类型的使用: <hibernate-mapping package="cn.model"> <class name="Department&quo ...
- C# 使用XmlDocument类对XML文档进行操作
原创地址:http://www.cnblogs.com/jfzhu/archive/2012/11/19/2778098.html 转载请注明出处 W3C制定了XML DOM标准.很多编程语言中多提供 ...
- 不同版本的 IIS 中使用 ASP.NET MVC(C#)【转】
由微软 ASP.NET 团队|2008 年 8 月 19 日 推特 在本教程中,您将学习在不同版本的 Internet Information Services 中如何使用 ASP.NET MVC 和 ...
- Android快捷支付SDK Demo resultStatus={4001};memo={參数错误};result={}问题
在支付宝中粘贴RSA公钥并提交,然后问题就完美攻克了...
- java:添加一条数据到数据库中文乱码
在数据库链接地址后面加上:characterEncoding=UTF8 如:jdbc\:mysql\://localhost\:3306/db_sjzdaj?relaxAutoCommit=true& ...
- DropDownList四级联动
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_dro ...