- 浏览: 73543 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
lijx127:
例1的输出结果应该是:this is toString
JAVA中tostring方法的作用 -
chengdaohuang:
不错 我正好在用这个方法的时候出了问题,苦于找不到解决思路 ...
Java中toArray的用法探究 -
javer:
...
Java中toArray的用法探究
一. .properties 文件的形式 ==========================================================
# 以下为服务器、数据库信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# 以下为数据库表信息
dbTable = mytable
# 以下为服务器信息
ip = 192.168.0.9
······
在上面的文件中我们假设该文件名为: test.properties 文件。其中 # 开始的一行为注释信息;在等号“ = ”左边的我们称之为 key ;等号“ = ”右边的我们称之为 value 。(其实就是我们常说的键 - 值对) key 应该是我们程序中的变量。而 value 是我们根据实际情况配置的。
二. JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法: 1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value 。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。 3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。
有了以上几个方法我们就可以对 .properties 文件进行操作了!
三.代码实例
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 ...{
outputFile = 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
# 以下为服务器、数据库信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# 以下为数据库表信息
dbTable = mytable
# 以下为服务器信息
ip = 192.168.0.9
······
在上面的文件中我们假设该文件名为: test.properties 文件。其中 # 开始的一行为注释信息;在等号“ = ”左边的我们称之为 key ;等号“ = ”右边的我们称之为 value 。(其实就是我们常说的键 - 值对) key 应该是我们程序中的变量。而 value 是我们根据实际情况配置的。
二. JDK 中的 Properties 类 Properties 类存在于胞 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法: 1. getProperty ( String key) , 用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value 。
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。 3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments) , 以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。
有了以上几个方法我们就可以对 .properties 文件进行操作了!
三.代码实例
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 ...{
outputFile = 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
发表评论
-
Java常用正则表达式
2010-09-26 21:46 700Java常用正则表达式 正则几个基本概念: 1.贪婪:+, ... -
Maven入门--概念与实例
2010-09-23 13:19 6171 关键名词 Project:任何您想build的事 ... -
Java Reflection (JAVA反射)
2010-09-23 13:11 583Java Reflection (JAVA反射) ... -
Java PreparedStatement
2010-09-23 13:05 2424该 PreparedStatement接口继承Statemen ... -
Java Statement
2010-09-23 13:03 914java Statement详细用法 创建 Statement ... -
Java面试题
2010-09-23 11:55 1328第一章 JAVA基础知识 §1.1 Java概述 1.完成在 ... -
从指定网址下载网页
2010-09-23 10:10 806import java.net.URL; import jav ... -
Java中equals和==的区别
2010-09-15 23:18 507值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈 ... -
java create xml
2010-09-08 23:10 734import java.io.FileOutputStream ... -
JAVA 转义字符
2010-09-08 23:07 1505转义序列 含义 \n 回车(\u000a) \t 水平制表 ... -
JAVA的位移运算巧方法
2010-09-06 22:23 614左移操作: x << n x可以是byte, ... -
对Java中四种XML解析技术之不完全测试
2010-09-05 22:32 581平时工作中,难免会遇到把XML作为数据存储格式。面对目前种类繁 ... -
Java this变量总结
2010-09-05 22:22 534this为一系统资源,只允许用户读而不允许写,它存放当前对象的 ... -
Timestamp基础知识及时间大小比较
2010-09-05 18:28 1383主要写一些与数据库连接时的日期处理,pstmt.setDate ... -
Java中ArrayList和LinkedList区别
2010-09-05 18:19 501一般大家都知道arraylist和linkedlist的大致区 ... -
Java中hashcode()方法和equals方法
2010-09-05 18:17 531hashcode这个方法是用来鉴定2个对象是否相等的。 ... -
Java中equals和==的区别
2010-09-05 18:13 580值类型是存储在内存中的堆栈(以后简称栈),而引用类型的变量在栈 ... -
Java反射测验代码
2010-09-05 18:10 495Java代码 package reflect; imp ... -
Java异常处理的一般性原则
2010-09-05 18:06 7051)首先,不要丢弃异常 ... -
Java 栈与堆都
2010-09-05 18:02 510栈与堆都是Java用来在Ram ...
相关推荐
java 读取properties文件代码
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
Java读取Properties文件的六种方法
JAVA读取properties的值,JAVA读取properties文件,JAVA读取properties的内容
java读写properties配置文件
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
java读写properties文件,解决系统找不到指定路径,解决写入后读取正常,但文件数据未更新问题
NULL 博文链接:https://xieke90.iteye.com/blog/2233046
java实现properties文件读取
java如何读取properties配置文件
主要介绍了java读取properties文件的方法,实例分析了java操作properties配置文件的相关技巧,需要的朋友可以参考下
java对properties配置文件的读和写
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
主要介绍了java读取properties文件的方法,实例分析了java读取在项目中与不在项目中properties文件的相关技巧,需要的朋友可以参考下
解决JAVA读取properties中文乱码问题
NULL 博文链接:https://duanfei.iteye.com/blog/565672