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);
    }

 

相关文章

暂无评论

none
暂无评论...