java键值对_Java 读写键值对

Properties类(读入写出键值对)是Map子类Map方法都能用publicstaticvoidmain(String[]args)throwsIOException{//读出Propertiespro=newProperties();FileInputStreamfis=newFileInputStream(“F:\\Demo.properties”);//Fi…

大家好,又见面了,我是你们的朋友全栈君。

Properties类(读入写出 键值对) 是Map子类Map方法都能用

public static void main(String[] args) throws IOException {

//读出

Properties pro=new Properties();

FileInputStream fis = new FileInputStream(“F:\\Demo.properties”);

//FileReader fr=new FileReader(“F:\\Demo.properties”);

pro.load(fis);//读取键值对 pro.load(fr);

String str=pro.getProperty(“name”);//取值

fis.close(); //fr.close();

//写入

Properties pro=new Properties();//创建集合

pro.setProperty(“name”,”lisi”);//写入键值对

pro.setProperty(“hobby”,”eat”);

pro.setProperty(“hobby”,”sleep”);//替换值

String str=pro.getProperty(“name”);//取值

Set set=pro.stringPropertyNames();//像keySet获取 键值集合

for(String key:set){//遍历

System.out.println(key+”…”+pro.getProperty(key));

}

FileOutputStream fos=new FileOutputStream(“F:\\aaa.properties”,true);

//FileWriter fw = new FileWriter(“F:\\aaa.properties”,true);

pro.store(fos, “person information”); //(文件路径,添加理由)

//pro.store(fw,”save data”);

fos.close();//5,关闭流

}

//父类方法:

Properties prop = new Properties();

prop.put(“CZBK001”, “zhangsan”);//添加映射关系

Set keys = prop.keySet();

for (Object key : keys) {

Object value = prop.get(key);

System.out.println(key + “=” + value);

}

Set> entrys = prop.entrySet();

for (Map.Entry entry : entrys) {

Object key = entry.getKey();

Object value = entry.getValue();

System.out.println(key + “=” + value);

}

PrintWriter pw = new PrintWriter(“d.txt”);//打印流对象

prop.list(pw);

pw.close();//释放资源

Properties类JDBC从文件读取信息

public static Connection getConn(){

Connection conn=null; //不是局部

Properties pro=new Properties();

try{

FileInputStream fis=new FileInputStream(“src/pro.properties”);

pro.load(fis); //相对路径 正斜杠

Class.forName(pro.getProperty(“driverClass”));//点出try catch

String url=pro.getProperty(“url”);

String username=pro.getProperty(“username”);

String password=pro.getProperty(“password”);//  空

conn=DriverManager.getConnection(url, username, password);//点add catch

}

catch (ClassNotFoundException e) {e.printStackTrace();}

catch (SQLException e) {e.printStackTrace();}

catch (IOException e) {e.printStackTrace();}

return conn;

}

src/pro.properties文件://无空格   密码空串(空格)

driverClass=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/guanjiapo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

username=root

password=

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126581.html原文链接:https://javaforall.net

赞 (1)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu server 文件共享

    ubuntu server 文件共享

    2021年8月20日
  • sp_executesql接收返回多个参数实例

    sp_executesql接收返回多个参数实例近日做项目中需要在EXEC执行Sql字符串时动态的传入参数并接收返回值,于是研究了一下SqlServer中sp_executesql的使用方法,并做了如下的例子。在使用sp_executesql动态传入与接收返回参数时需注意以下事项,以避免大家走弯路。例子中@SQLString,@ParmDefinition一定要使用NVARCHAR类型,否则会报“过程需要参数××××为ntext/n

    2022年5月11日
  • 分布式锁—-数据库和redis实现分布式锁

    分布式锁—-数据库和redis实现分布式锁

    2021年8月3日
  • php网站判断用户是否是手机访问的方法

    php网站判断用户是否是手机访问的方法

    2021年10月15日
  • PR曲线详解

    PR曲线详解目录PR曲线概念precision(精准率)和recall(召回率)PR曲线功能说明PR曲线概念PR曲线中的P代表的是precision(精准率),R代表的是recall(召回率),其代表的是精准率与召回率的关系,一般情况下,将recall设置为横坐标,precision设置为纵坐标。precision(精准率)和recall(召回率)上述中介少了PR曲线的实质代表为precision(精准率)和recall(召回率),但是这二者是什么呢?下面咱们进行相关的讲述。首先,我们了解一下混淆矩阵,如下表

    2022年6月30日
  • 写给大忙人看的操作系统

    文章主要结构图如下操作系统现代计算机系统由一个或多个处理器、主存、打印机、键盘、鼠标、显示器、网络接口以及各种输入/输出设备构成。然而,程序员不会直接和这些硬件打交道,而且每位程序员不可能会掌握所有计算机系统的细节,这样我们就不用再编写代码了,所以在硬件的基础之上,计算机安装了一层软件,这层软件能够通过响应用户输入的指令达到控制硬件的效果,从而满足用户需求,这种软件称之为操作系统,它的…

    2022年4月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号