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

方法如下:

public static Map fileToMap(String path){
        Map 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当中)