private void createFile() { File sdCard= Environment.getExternalStorageDirectory(); String fileName = "data.txt"; File file = new File(sdCard, fileName) ; if(!file.exists()){ try { file.createNewFile(); String oldPath = file.getAbsolutePath(); String newPath = ""; newPath = oldPath.replace(fileName, "other.txt"); renameFile(oldPath, newPath); //file is create } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { String oldPath = file.getAbsolutePath(); if(!TextUtils.isEmpty(oldPath)) { String newPath = file.getAbsolutePath() + "other.txt"; newPath = oldPath.replace(fileName, "other.txt"); renameFile(oldPath, newPath); } } } /** * oldPath 和 newPath必须是新旧文件的绝对路径 * */ private void renameFile(String oldPath, String newPath) { if(TextUtils.isEmpty(oldPath)) { return; } if(TextUtils.isEmpty(newPath)) { return; } File file = new File(oldPath); file.renameTo(new File(newPath)); }
android 重命名文件
最新推荐文章于 2021-09-23 19:03:03 发布