集合中的数据存入到文件中,文件中的数据读取到集合中

* Map<String ,Integer> map = new LinkedHashMap<>();
* map.put(“摩卡”,30);
* map.put(“卡布奇诺”,27);
* map.put(“拿铁”,27);
* map.put(“香草拿铁”,30);
*   ||
* \ || /
*   \/
* 摩卡=30
* 卡布奇诺=27
* 拿铁=27
* 香草拿铁=30

将集合中的数据存入文件中

package MONA.demo01_作业题;

import java.io.FileOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        Map<String,Integer> map = new LinkedHashMap<>();
        map.put("摩卡",30);
        map.put("卡布奇诺",27);
        map.put("拿铁",27);
        map.put("香草拿铁",30);

        FileOutputStream fos = new FileOutputStream("1.txt");

        for(String key : map.keySet()){
            fos.write(key.getBytes());
            fos.write("=".getBytes());
            fos.write(map.get(key).toString().getBytes());
            fos.write("\r\n".getBytes());
        }
        fos.close();
        System.out.println(map);
    }
}

 经过字符缓冲流的优化

package MONA.demo08_作业优化题;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.LinkedHashMap;
import java.util.Map;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        Map<String,Integer> map = new LinkedHashMap<>();
        map.put("摩卡",30);
        map.put("卡布奇诺",27);
        map.put("拿铁",27);
        map.put("香草拿铁",30);

        BufferedWriter bw = new BufferedWriter(new FileWriter("1.txt"));
        for(String key : map.keySet()){
            bw.write(key+"="+map.get(key));
            bw.newLine();//换行
        }
        bw.close();
        System.out.println("程序结束");
    }
}

将保存到文件中的数据读取到集合中

package MONA.demo01_作业题;

import java.io.FileInputStream;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 将保存到文件中的数据读取到集合中
 */
public class Demo02 {
    public static void main(String[] args) throws Exception {
        Map<String ,Integer> map = new LinkedHashMap<>();
        FileInputStream fis = new FileInputStream("1.txt");
        byte[] bytes = new byte[1024];
        int count = fis.read(bytes);
        String str = new String(bytes,0,count);
        String[] lines = str.split("\r\n");

        for (String line : lines) {
            String[] l = line.split("=");
            //String --> int Integer
            map.put(l[0],Integer.parseInt(l[1]));
        }
        System.out.println(map);
    }
}

 优化过后

package MONA.demo08_作业优化题;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedHashMap;
import java.util.Map;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        Map<String, Integer> map = new LinkedHashMap<>();
        BufferedReader br = new BufferedReader(new FileReader("1.txt"));
        String line  = null;;
        while ((line = br.readLine()) != null){
            String[] l = line.split("=");
            map.put(l[0],Integer.parseInt(l[1]));
            System.out.println(line);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值