android 重命名文件
16lz
2021-01-23
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根据uri或文件绝对路径获取文件基本信息
- Android 根据Video绝对路径获取其缩略图
- android 安卓 开发 图片库获得图片的绝对路径
- PHP获取绝对路径dirname(__FILE__)和__DIR__比较
- XHTML 相对路径与绝对路径
- 如何在Linux中以编程方式获取给定相对路径的绝对路径?
- 确定shell程序中的相对路径或绝对路径