1.通过代码了解一哈:

 package com.etc;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/*
* 作用:读写资源配置文件
* 方法:
* 1.存取方法
* setProperty(String key,String value);键与值只能为字符串
* getProperty(String key);存在则返回值,不存在返回空
* getProperty(String key,defaultValue);如果不存在返回默认值defaultValue
* 2.存储到文件中
* (1)生成的文件后缀名为.properties;
* store(OutputStream out,String comments);
* store(Writer writer,String comments);
* (2)生成的文件后缀名为.xml
* storeToXML(OutputStream out,String comments);默认UTF-8字符集
* storeToXML(OutputStream out,String comments,String encoding);指定字符集
* 3.从文件中读取内容
* (1).properties文件读取
* load(InputStream inStream);
* load(Reader reader)
* (2).xml文件读取
* loadFromXML(InputStream inStream)
* 4.相对路径与绝对路径:
* 相对路径:当前项目,工程
* 绝对路径:具体盘符
* 5.类路径加载资源文件
* 类所在的根路径uri:
* (1)类名.class.getResourceAsStream("/uri");
* (2)Thread.currentThread().getContextClassLoader().getResourceAsStream("/uri");
*/
public class TestProperties { public static void main(String[] args) throws FileNotFoundException, IOException { //利用Properties存取信息,以后可以用来改进连接数据库的方法,提高效率
Properties pro=new Properties();
//注册驱动
pro.setProperty("driver", "com.mysql.jdbc.Driver");
//获取连接:url:jdbc:mysql://连接主机IP:端口号/数据库名字
pro.setProperty("url", "jdbc:mysql://localhost:3306/TEST");
//用户名与密码
pro.setProperty("password", "121515");
pro.setProperty("user", "root");
//获取value
System.out.println(pro.getProperty("url","空"));
System.out.println(pro.getProperty("test","空"));
//存储到桌面
pro.store(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件1.properties")), "ABC");
pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/文件2.xml")), "测试文件","GBK");
pro.storeToXML(new FileOutputStream(new File("文件3.xml")), "测试文件","GBK"); //从存储文件中获取信息并存入pro2对象中
Properties pro2=new Properties();
pro2.load(new FileInputStream (new File("C:/Users/Administrator/Desktop/文件1.properties")));
System.out.println("文件读取内容为:"+pro2.getProperty("url"));
} }

2.效果截图:

控制台输出:

项目:

桌面:

打开文件:

接下来利用面向对象的理念实现一个教师信息写入文件的小案例:

1.新建一个实体类,用于存放数据

 package com.test;
//实体类用于存放数据
public class Teacher {
private String name;
private int id;
private String date; public Teacher(String name, int id, String date) {
super();
this.name = name;
this.id = id;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "姓名:"+name+" 编号:"+id+" 入教时间:"+date;
} }

2.测试类,场景模拟

 package com.test;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; /*
* 简单运用面向对象实现教师信息写入配置文件加以保存
* 总共分为两步:
* 1.输入数据
* 2.处理数据并将其存储在桌面的文件下
*/
public class teacherInfoWriteIn { public static void main(String[] args) throws FileNotFoundException, IOException {
//获取数据并将数据存取至桌面的文件1.xml文件中
Properties pro=setData();
pro.storeToXML(new FileOutputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")), "教师信息表","GBK");
//将桌面上的文件1里面的信息获取出来
Properties pro2=new Properties();
pro2.loadFromXML(new FileInputStream(new File("C:/Users/Administrator/Desktop/Test1.xml")));
System.out.println(pro2.getProperty("张老师信息"));
System.out.println(pro2.getProperty("李老师信息"));
System.out.println(pro2.getProperty("王老师信息"));
System.out.println(pro2.getProperty("刘老师信息"));
}
//数据初始化输入
public static Properties setData() {
Properties pro=new Properties();
Teacher t1=new Teacher("张三",11152,"2004-5");
Teacher t2=new Teacher("李四",13157,"2007-4");
Teacher t3=new Teacher("王五",12456,"1998-5");
Teacher t4=new Teacher("刘六",15478,"1999-5");
pro.put("张老师信息",t1.toString());
pro.put("李老师信息",t2.toString());
pro.put("王老师信息",t3.toString());
pro.put("刘老师信息",t4.toString());
return pro;
}
}

效果截图:

控制台:

桌面:

ps:文章待完善,先简单了解学习一下,如有问题欢迎大佬指点。

java-初识Properties的更多相关文章

  1. Android中使用java.util.Properties犯的错

    今天尝试使用java.util.Properties来保存应用配置,然而遇到了好几个问题,对于熟悉此内容的来说可能都是猪一样的错误,但难免有像我一样的新手再次遇到,希望此文能有所帮助. 错误1 jav ...

  2. java 读写properties (配置)文件

    Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...

  3. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  4. java读写Properties属性文件公用方法

    Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件. 它提供了几个主要的方法: 1. getProperty ( String ...

  5. Java关于Properties用法(二)——替换配置文件中的参数

    上一章讲了配置文件的基本用法,虽然上一章已经可以解决一些需求,但还不些不足之处.假如,配置文件里面的字符串有一部分需要经常变动,另外一些不需要,上一章的方法就不方便了,所以这章主要讲如何在配置文件中使 ...

  6. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

  7. java 读取properties文件

    import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Ge ...

  8. java 读写properties

    网速不好:先贴上资料: Java配置文件Properties的读取.写入与更新操作 [Spring] - Property注入 http://www.360doc.com/content/14/073 ...

  9. java读取properties配置文件总结

    java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...

  10. Java关于Properties用法的总结(一)

    最近项目中有一个这样的需求,要做一个定时任务功能,定时备份数据库的操表,将表数据写入txt文件.因为文件的读写路径可能需要随时改动,所以写死或者写成静态变量都不方便,就考虑使用配置文件,这里总结些配置 ...

随机推荐

  1. 马哥k8s

    https://pan.baidu.com/s/1BAX-j54bLcmWF-0ei-c-pw 31y6

  2. .NET Standard 2.0正式发布了

    亦可赛艇 前天(2017年8月14日),.NET Standard 2.0正式版终于发布了,与之相配套的.NET Core 2.0也同时正式发布,真是令人振奋. 详情请看:https://blogs. ...

  3. 3种方法来在Linux电脑上查找文件

    如果你不太了解Linux命令,那么在Linux系统里查找文件是比较困难的.只要使用多种不同的终端命令,可以很快地找到文件.Linux命令比其它操作系统的搜索功能更加强大,掌握这些命令就能你完全控制这些 ...

  4. [Swift]LeetCode333. 最大的二分搜索子树 $ Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  5. [Swift]LeetCode336. 回文对 | Palindrome Pairs

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  6. [Swift]LeetCode443. 压缩字符串 | String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  7. [Swift]LeetCode801. 使序列递增的最小交换次数 | Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  8. Node.js 多版本安装

    Node.js 多版本安装 Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine(Node.js 是一个基于 ...

  9. javascript 使用小技巧总结

    按位取反 ~a 即:返回 -(a+1),会去掉小数点. let a = 3.14; let b = ~a; //b = -(3.14+1) 取整 为-4: let c = ~b; //c = -(-4 ...

  10. 面试前必知Redis面试题—缓存雪崩+穿透+缓存与数据库双写一致问题

    今天来分享一下Redis几道常见的面试题: 如何解决缓存雪崩? 如何解决缓存穿透? 如何保证缓存与数据库双写时一致的问题? 一.缓存雪崩 1.1什么是缓存雪崩? 回顾一下我们为什么要用缓存(Redis ...