android, How to rename a file?

前端 未结 9 2092
Happy的楠姐
Happy的楠姐 2020-11-28 09:58

In my application, I need to record video. Before start of recording in I\'m assigning a name and directory to it. After recording is finished user has ability to rename his

相关标签:
9条回答
  • 2020-11-28 10:07

    Working example...

       File oldFile = new File("your old file name");
        File latestname = new File("your new file name");
        boolean success = oldFile .renameTo(latestname );
    
       if(success)
        System.out.println("file is renamed..");
    
    0 讨论(0)
  • 2020-11-28 10:12

    Provide target File object with different file Name.

    // Copy the source file to target file.
    // In case the dst file does not exist, it is created
    void copy(File source, File target) throws IOException {
    
        InputStream in = new FileInputStream(source);
        OutputStream out = new FileOutputStream(target);
    
        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
    
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    
        in.close();
        out.close();
    }
    
    0 讨论(0)
  • 2020-11-28 10:17

    Use this method to rename a file. The file from will be renamed to to.

    private boolean rename(File from, File to) {
        return from.getParentFile().exists() && from.exists() && from.renameTo(to);
    }
    

    Example code:

    public class MainActivity extends Activity {
        private static final String TAG = "YOUR_TAG";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            File currentFile = new File("/sdcard/currentFile.txt");
            File newFile = new File("/sdcard/newFile.txt");
    
            if (rename(currentFile, newFile)) {
                //Success
                Log.i(TAG, "Success");
            } else {
                //Fail
                Log.i(TAG, "Fail");
            }
        }
    
        private boolean rename(File from, File to) {
            return from.getParentFile().exists() && from.exists() && from.renameTo(to);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 10:17

    This is what I ended up using. It handles the case where there is an existing file with the same name by adding an integer to the file name.

    @NonNull
    private static File renameFile(@NonNull File from, 
                                   @NonNull String toPrefix, 
                                   @NonNull String toSuffix) {
        File directory = from.getParentFile();
        if (!directory.exists()) {
            if (directory.mkdir()) {
                Log.v(LOG_TAG, "Created directory " + directory.getAbsolutePath());
            }
        }
        File newFile = new File(directory, toPrefix + toSuffix);
        for (int i = 1; newFile.exists() && i < Integer.MAX_VALUE; i++) {
            newFile = new File(directory, toPrefix + '(' + i + ')' + toSuffix);
        }
        if (!from.renameTo(newFile)) {
            Log.w(LOG_TAG, "Couldn't rename file to " + newFile.getAbsolutePath());
            return from;
        }
        return newFile;
    }
    
    0 讨论(0)
  • 2020-11-28 10:18
    public void renameFile(File file,String suffix) {
    
        String ext = FilenameUtils.getExtension(file.getAbsolutePath());
        File dir = file.getParentFile();
        if(dir.exists()) {
            File from = new File(dir, file.getName());
            String name = file.getName();
            int pos = name.lastIndexOf(".");
            if (pos > 0)
                name = name.substring(0, pos);
            File to = new File(dir, name + suffix + "." + ext);
            if(from.exists())
                from.renameTo(to);
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 10:28

    In your code:

    Shouldn't it be :

    File from = new File(directory, currentFileName);

    instead of

    File from = new File(directory, "currentFileName");


    For safety,

    Use the File.renameTo() . But check for directory existence before renaming it!

    File dir = Environment.getExternalStorageDirectory();
    if(dir.exists()){
        File from = new File(dir,"from.mp4");
        File to = new File(dir,"to.mp4");
         if(from.exists())
            from.renameTo(to);
    }
    

    Refer: http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29

    0 讨论(0)
提交回复
热议问题