美文网首页Android知识
Android读取文件并保存到Map当中

Android读取文件并保存到Map当中

作者: 雪凌风 | 来源:发表于2018-02-08 15:13 被阅读0次

方法如下:

public static Map<String, String> fileToMap(String path){
        Map<String,String> map = new HashMap<>();
        File file = new File(path);
        if (!file.exists()){
            return null;
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String temp = "";
            int line = 1;
            while ((temp = reader.readLine())!=null){
                String[] arr = temp.split(":");
                map.put(arr[0], arr[1]);
                line++;
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

相关文章

网友评论

    本文标题:Android读取文件并保存到Map当中

    本文链接:https://www.haomeiwen.com/subject/nfyttftx.html