现在的位置: 首页 > 综合 > 正文

Android中的meminfo(二):用代码来读取文件信息

2018年05月16日 ⁄ 综合 ⁄ 共 688字 ⁄ 字号 评论关闭

1. 简介:

上篇,介绍了meminfo和cpuinfo文件,本篇给出程序例程,用代码的方式来获取它们的值。

以memino为例。

2. 代码:

public static List<Long> getMeminfo() {
        List<Long> memInfoList = new ArrayList<Long>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/meminfo")), 1000);
            String load = null;
            while ((load = reader.readLine()) != null) {
                long size = 0l;
                String[] toks = load.split(":");
                String sizeBuf = toks[1].trim();
                String[] sizeBufToks = sizeBuf.split(" ");
                size = Long.parseLong(sizeBufToks[0]); // kb
                memInfoList.add(size);
            }
            reader.close();
            return memInfoList;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }

    }

是不是很容易啊。

抱歉!评论已关闭.