Properties
2015-07-22 01:35
369 查看
@(笔记)[MarkDown|集合|我的博客|每日一类]
07.21
Properties类
方法的使用
Properties 综合使用
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
新建一个文件
内容为
键1=值1
键2=值2
…
使用此类可以对文件数据进行持久化保存
07.21
Properties类
方法的使用
Properties 综合使用
Properties类
public class Propertiesextends Hashtable<Object,Object>
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
新建一个文件
内容为
键1=值1
键2=值2
…
使用此类可以对文件数据进行持久化保存
方法的使用
load(InputStream inStream);从输入流中读取属性列表(键和元素对)。
Properties pro = new Properties(); pro.load(new FileInputStream(相对路径或绝对路径));//此方法将抛出FileNotFoundException
getPropertiy(String key)使用指定的键在此属性列表中搜索属性,返回String
getProperty(String key, String defaultValue)用指定的键在属性列表中搜索属性。返回String
setProperty(String key, String value)如果存在了这个键,则更改对应的值,如果不存在,则添加这个属性,这个方法是调用Hashtable的put的结果
public void store(OutputStream out, String comments) throws IOException写入文件中
props.store(new FileOutputStream(相对路径或绝对路径), 说明);
Properties 综合使用
/* * @Author: Code.Ai * @Date: 2015-07-21 23:57:11 * @Last Modified by: Code.Ai * @Last Modified time: 2015-07-22 00:51:58 */ import java.io.*; import java.util.*; public class PropertiesTest { static Properties props = new Properties(); public static void main(String[] args) { //读文件 Read(); System.out.println(props.getProperty("Teacher")); //添加数据 //使用setProperties()方法,如果文件中没有相同的键,则添加,如果有相同的,就覆盖 props.setProperty("Teacher","胡老大"); props.setProperty("Student","Code"); System.out.println(props.getProperty("Student")); Save(); //删除全部数据 //直接在没有加载文件的情况下使用store()方法存入空数据到文件中 Read(); //存入Sudtent对象 for(int i = 0 ; i < 3; i++){ Scanner scan = new Scanner(System.in); System.out.println("请输入学生名字:"); String name = scan.next(); System.out.println("请输入学生年龄:"); int age = scan.nextInt(); System.out.println("请输入学生成绩"); int score = scan.nextInt(); Student stu = new Student(name,age,score); props.setProperty(stu.getName(), stu.getName() + "&" + stu.getAge() + "&" + stu.getScore()); } Save(); //删除指定的键 直接调用Map类的remove方法 props.remove("Student"); Save(); //构造学生对象 //把值取出来分解,然后赋值给学生 //调用继承Map的values()方法,取得所有的值 Collection<String> cl = props.values(); for(String str : cl){ System.out.println(str); //拆分字符串 } } public static void Read(){ try { props.load(new FileInputStream("data.properties")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //存文件 public static void Save(){ try { props.store(new FileOutputStream("data.properties"), null); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- Linux下5种IO模型的小结
- 【OpenGL】“我叫MT”纯手工3D动画制作之2——建立模型
- 【OpenGL】“我叫MT”纯手工3D动画制作之1——基础介绍
- Centos6.6下安装MySQL5.6
- Linux-config-prefix
- 大叔开始学Linux、学安卓了!!!
- linux下使用yum安装mysql
- Linux安装MySQL的两种方法 普通安装法 rpm安装
- 通过mod_python配置运行在Apache上的Django框架
- 在Apache服务器上同时运行多个Django程序的方法
- 使用相同的Apache实例来运行Django和Media文件
- 简单的Apache+FastCGI+Django配置指南
- VS2010发布Web网站技术攻略
- select、option
- Linux系统下将php和MySQL命令加入到环境变量中的方法
- Linux中vi编辑器的使用详解
- shell 之点滴记录
- maven 镜像网站
- COMPUTER HARDWARE OPENCART 主题模板 ABC-0059
- linux程序设计——套接字连接(第十五章)