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();
}
Android应用程序里创建文件及改变权限
最新推荐文章于 2024-08-11 03:58:23 发布