gpt4 book ai didi

java - 从Android手机的SD卡中删除文件

转载 作者:行者123 更新时间:2023-11-29 21:36:40 26 4
gpt4 key购买 nike

我想删除 SD 卡上名为“playerdata.txt”的文件。以下代码无效

{
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>

显化

最佳答案

添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

在AndroidManifest.xml文件中

关于java - 从Android手机的SD卡中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com