2013-08-16 64 views
1

我想删除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>

体现

+0

我相信,文件路径是错误的... – sanbhat

+0

试试这个'档案文件=新的文件(android.os.Environment.getExternalStorageDirectory()”/GameHacker/playerdata.txt“); if(file.exists()) file.delete(); }'并确保您有'使用权限android:name =”android.permission.WRITE_EXTERNAL_STORAGE'清单 – Raghunandan

回答

2

泰添加

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

文件

+0

谢谢你的回答,但我已经完成了 – Mahfa

0

可能有很多原因,未为文件被删除。其中之一是你的应用程序没有SD卡的写入权限。尝试包括权限 机器人:名字=“android.permission.WRITE_EXTERNAL_STORAGE

+0

谢谢你的回答,但我已经将它添加到清单 – Mahfa