我想删除SD卡上名为“playerdata.txt”的文件。下面的代码是不工作从Android手机的SD卡中删除文件
{
File file = new File("/sdcard/GameHacker/playerdata.txt");
file.delete();
}
我的问题是我要复制“playerdata.txt”,以所谓的GameHacker该文件夹,我用这个代码
Context Context = getApplicationContext();
String DestinationFile = "/sdcard/GameHacker/playerdata.txt";
if (!new File(DestinationFile).exists()) {
try {
CopyFromAssetsToStorage(Context, "playerdata", DestinationFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void CopyFromAssetsToStorage(Context Context, String SourceFile, String DestinationFile) throws IOException {
InputStream IS = Context.getAssets().open(SourceFile);
OutputStream OS = new FileOutputStream(DestinationFile);
CopyStream(IS, OS);
OS.flush();
OS.close();
IS.close();
}
private void CopyStream(InputStream Input, OutputStream Output) throws IOException {
byte[] buffer = new byte[5120];
int length = Input.read(buffer);
while (length > 0) {
Output.write(buffer, 0, length);
length = Input.read(buffer);
}
}
,它工作正常,但第二次它不会取代它,我想先删除然后复制我添加
> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
体现
我相信,文件路径是错误的... – sanbhat
试试这个'档案文件=新的文件(android.os.Environment.getExternalStorageDirectory()”/GameHacker/playerdata.txt“); if(file.exists()) file.delete(); }'并确保您有'使用权限android:name =”android.permission.WRITE_EXTERNAL_STORAGE'清单 – Raghunandan