简体   繁体   中英

Java - Move and Rename all files from one destination to another

I have files in one folder, i want to move them into the another folder but also and to rename them (with some static prefix value which should be added)

I succeed to list all files from the source directory but I cannot find move method when getting files[i] and I also cannot find how to rename and move files to another folder in the same time.

Can someone tell me what I should add in getFiles method in order to move and rename.

This is my class.

import java.io.File;

public class CopyTest {

    static File mainFolder = new File("F:\\TestCopy");
    static File destinationFolder = new File("F:\\TestCopy2");
    public String prefix="PREFIX";

    public static void main(String[] args)
    {
        CopyTest lf = new CopyTest();
        lf.getFiles(lf.mainFolder);

        long fileSize = mainFolder.length();

            System.out.println("File size in KB is : " + (double)fileSize/1024);

    }
    public void getFiles(File f){
        File files[];
        if(f.isFile())
            System.out.println(f.getAbsolutePath());
        else{
            files = f.listFiles();
            for (int i = 0; i < files.length; i++) {


                getFiles(files[i]);

            }
        }
    }
}

You can use file.renameto() for moving and renaming.

Sample -

import java.io.File;

public class MoveFileExample
{
    public static void main(String[] args)
    {
        try{

           File afile =new File("C:\\folderA\\Afile.txt");

           if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
            System.out.println("File is moved successful!");
           }else{
            System.out.println("File is failed to move!");
           }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

How about yourFile.renameTo(new File("C://newpath.txt')); ?

how to move file from one location to another location in java?

You can rename it in the new File(...) , so get the file name and add your prefix.

You can write this way

  import java.io.File;

public class CopyTest
{

static File mainFolder = new File("F:\\TestCopy");
static File destinationFolder = new File("F:\\TestCopy2");
public String prefix = "PREFIX";

public static void main(String str[])
{
    CopyTest lf = new CopyTest();
    lf.getFiles(lf.mainFolder);

    long fileSize = mainFolder.length();

    System.out.println("File size in KB is : " + (double) fileSize / 1024);

}

public void getFiles(File f)
{
    File files[];
    if (f.isFile())
    {
        f.renameTo(new File(destinationFolder + "\\" + prefix + f.getName()));
    }
    else
    {
        files = f.listFiles();
        for (int i = 0; i < files.length; i++)
        {

            getFiles(files[i]);

        }
    }
}

}

Copy File From One Folder To Another Then Remove Source file

/**
 * Copy File From One Folder To Another Folder
 * Then Delete File
 * @param sourceFile
 * @param destFile
 * @throws IOException 
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(sourceFile);
        out = new FileOutputStream(destFile);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
    } catch(Exception e){
        e.printStackTrace();
    }
    finally
    {
        in.close();
        out.close();
        sourceFile.delete();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM