我正在尝试在Android模拟器中将apk写入/ system / app位置。这是我的代码:
File path = Environment.getRootDirectory();
File fileToWrite = new File(path, "/" + fileName);
byte[] buff = new byte[1024];
int l = 0;
// write buffer to file
FileOutputStream fos = new FileOutputStream(fileToWrite);
while((l = zis.read(buff)) > 0){
fos.write(buff,0, l);
}
fos.close();
我的AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
但是,我收到以下错误消息:
java.io.FileNotFoundException:/system/MainApp-debug.apk(只读) 文件系统) 06-08 08:51:59.858 2921-3160 / com.mainapp W / System.err:at java.io.FileOutputStream.open0(Native Method) 在java.io.FileOutputStream.open(FileOutputStream.java:287)
我设法使用相同的代码写入/mnt/sdcard/Download
文件夹。但我不确定为什么它无法写入/system/app
文件夹。
有什么想法吗?谢谢!
答案 0 :(得分:0)
这是我用来创建路径的代码:
public static File root = android.os.Environment
.getExternalStorageDirectory();
public static String path = root.getAbsolutePath() + "/" + directoryName
+ "/";
public static boolean CreateDirectory() {
boolean ret = false;
path = root.getAbsolutePath() + "/" + directoryName + "/";
File folderExisting = new File(root.getAbsolutePath(), directoryName);
if (folderExisting.exists()) {
ret = true;
} else {
ret = folderExisting.mkdir();
// ret = false;
}
return ret;
}
然后保存东西:
public static boolean SaveToSD(List<String> data, String fileName) {
// File root = android.os.Environment.getExternalStorageDirectory();
boolean ret = false;
CreateDirectory();
File file = new File(path, fileName);
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
for (int idx = 0; idx < data.size(); idx++) {
if (data.get(idx) != null)
pw.println(data.get(idx));
}
pw.flush();
pw.close();
f.close();
ret = true;
} catch (FileNotFoundException e) {
ret = false;
e.printStackTrace();
} catch (IOException e) {
ret = false;
e.printStackTrace();
}
return ret;
}
希望它可以帮到你
和对象:
FileOutputStream fos = new FileOutputStream("file");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(mb);
oos.close();