Android (/sys/路径)属性文件节点值的读写

站长的个人作品

Android (/sys/路径)属性文件节点值的读写,在朋友代码基础上增加了finally 代码。如下,大致如此

    /**
     * 文件的读取权限记得添加咯
     */

    /**
     * 某个节点写入值
     *
     * @param value
     */
    public static void write(int value) {
        BufferedWriter bufWriter = null;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(SYS_PATH);
            bufWriter = new BufferedWriter(fileWriter);
            bufWriter.write(String.valueOf(value));  // 写操作
            bufWriter.close();
            Log.d(TAG, "write success to value=" + value);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "can't write the " + SYS_PATH);
        } finally {
            try {
                if (null != bufWriter) {
                    bufWriter.close();
                }
                if (null != fileWriter) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return;
    }

    /**
     * 读取某个节点的值
     *
     * @return
     */
    public static int read() {
        String value = "0";
        BufferedReader bufferedReader = null;
        FileReader fileReader = null;
        try {
            fileReader = new FileReader(SYS_PATH);
            bufferedReader = new BufferedReader(fileReader);
            value = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != fileReader) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Log.d(TAG, "read value=" + value);
        return Integer.parseInt(value);
    }

 

温馨提示:文章内容系作者个人观点,不代表博客志对观点赞同或支持。
版权声明:本文为投稿文章,感谢 125啦读书导航(125la.com) 的投稿,欢迎分享本文,转载请保留出处!
站长的个人作品
125la导航_独立博客导航平台

发表评论


表情

或者微信联系我