您的位置:首页 > 移动开发 > Android开发

Android应用程序里创建文件及改变权限

2013-04-25 18:11 141 查看
String dir = "/data/data/com.example.helloyude/files/";
String fileName = "mac_address.txt";
String macAddress = "xx:xx:xx:xx:xx";
File destDir = new File(dir);
if (!destDir.exists()) {
destDir.mkdirs();
}

// 修改权限
FileOutputStream fos;

try {
//方法1.增加其全局可读写权限
fos = openFileOutput(fileName, MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
byte[] bytes = macAddress.getBytes();
fos.write(bytes);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//方法2,直接更改文件权限
Process p;
int status = -1;
try {
p = Runtime.getRuntime().exec("chmod 777 " + dir + fileName);
status = p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (status == 0) {
Toast.makeText(this, "chmod succeed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "chmod failed", Toast.LENGTH_LONG).show();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: