Java - 将所有文件从一个目的地移动并重命名到另一个目的地

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

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

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

谁能告诉我应该在 getFiles 方法中添加什么以便移动和重命名。

这是我的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]);

            }
        }
    }
}

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

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

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

您可以使用 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();
        }
    }
}

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

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

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html 尝试 class 文件 move() 方法。

可以这样写

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

        }
    }
}

}