I want to rename an context private file created with openFileOutput() but I don't know how...
I tried that:
File file = getFileStreamPath(optionsMenuView.getPlaylistName()); // this file already exists
try {
FileOutputStream outStream = openFileOutput(newPlaylistName, Context.MODE_WORLD_READABLE); // i create a new file with the new name
outStream.close();
}
catch (FileNotFoundException e) {
Log.e(TAG, "file not found!");
e.printStackTrace();
}
catch (IOException e) {
Log.e(TAG, "IO exception");
e.printStackTrace();
}
Log.e(TAG, "rename status: " + file.renameTo(getFileStreamPath(newPlaylistName))); //it return t开发者_运维知识库rue
This code throw FileNotFoundException but the documentation said "Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist." so the new file should be created on disk. The problem: When I try to read from the new renamed file I got FileNotFoundException!
Thank you!
Here's a method to rename a file stored in your app's private store:
// Renames a file stored in the application's private store.
public static void RenameAppFile(Context context, String originalFileName, String newFileName) {
File originalFile = context.getFileStreamPath(originalFileName);
File newFile = new File(originalFile.getParent(), newFileName);
if (newFile.exists()) {
// Or you could throw here.
context.deleteFile(newFileName);
}
originalFile.renameTo(newFile);
}
Why not just use the File
class's renameTo()
method?
精彩评论