java map

java中map就是一个对象对应着另一个对象

name = asas
id = 001

像这样。

在java中要使用的话要用到Properties。

首先要确定路径

public class Gongju {
    public static void main(String[] args) {
        String path = "D:/i.txt";
        try {
            FileInputStream fis = new FileInputStream(path);
            Properties pro = new Properties();
            pro.load(fis);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

先用path确定路径,然后打开流,将流中了内容放到Properties中,然后就可以用getProperty方法查找想要的内容了

public class Gongju {
    public static void main(String[] args) {
        String path = "D:/i.txt";
        try {
            FileInputStream fis = new FileInputStream(path);
            Properties pro = new Properties();
            pro.load(fis);
            String name = pro.getProperty("name");
            String id = pro.getProperty("id");
            System.out.println(name);
            System.out.println(id);
            fis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

我i.txt中的内容是

name = asas
id = 001

运行结果

asas
001

Process finished with exit code 0

如果你文件中放了多个name和id,那么会打印出最后一个name,因为文件中包含了两个相同的键"name",但对应的值不同。在Properties对象中,后面出现会覆盖前面的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值