Java - 将所有文件从一个目标移动并重命名为另一个目标

时间:2016-09-16 11:55:40

标签: java file move

我有一个文件夹中的文件,我想将它们移动到另一个文件夹中,但也要重命名它们(带有一些应添加的静态前缀值)

我成功列出了源目录中的所有文件,但是在获取move时我找不到files[i]方法,而且我也找不到如何在同一时间重命名和移动文件到另一个文件夹。 / p>

有人可以告诉我应该在getFiles方法中添加什么来移动和重命名。

这是我的班级。

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]);

            }
        }
    }
}

5 个答案:

答案 0 :(得分:3)

您可以使用file.renameto()进行移动和重命名。

样本 -

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();
        }
    }
}

答案 1 :(得分:2)

yourFile.renameTo(new File("C://newpath.txt'));怎么样?

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

您可以在new File(...)中重命名,因此请获取文件名并添加前缀。

答案 2 :(得分:1)

答案 3 :(得分:1)

你可以这样写

  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]);

        }
    }
}

}

答案 4 :(得分:0)

将文件从一个文件夹复制到另一个文件夹然后删除源文件

/**
 * 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();
    }
}
相关问题