我们能把test。txt重命名为test1.txt吗?

如果test1.txt存在,它会重命名吗?

如何将其重命名为已经存在的test1.txt文件,以便将test.txt的新内容添加到其中以供以后使用?


当前回答

这是我的代码重命名多个文件在一个文件夹成功:

public static void renameAllFilesInFolder(String folderPath, String newName, String extension) {
    if(newName == null || newName.equals("")) {
        System.out.println("New name cannot be null or empty");
        return;
    }
    if(extension == null || extension.equals("")) {
        System.out.println("Extension cannot be null or empty");
        return;
    }

    File dir = new File(folderPath);

    int i = 1;
    if (dir.isDirectory()) { // make sure it's a directory
        for (final File f : dir.listFiles()) {
            try {
                File newfile = new File(folderPath + "\\" + newName + "_" + i + "." + extension);

                if(f.renameTo(newfile)){
                    System.out.println("Rename succesful: " + newName + "_" + i + "." + extension);
                } else {
                    System.out.println("Rename failed");
                }
                i++;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

并运行它作为一个例子:

renameAllFilesInFolder("E:\\Downloads\\Foldername", "my_avatar", "gif");

其他回答

我不喜欢java.io.File.renameTo(…),因为有时它不重命名文件,你不知道为什么!它只返回true (false)如果失败,它不会抛出异常。

另一方面,java.nio.file.Files.move(…)更有用,因为它会在失败时抛出异常。

Files.move(file.toPath(), fileNew.toPath()); 

工作,但只有当你关闭(或自动关闭)所有使用的资源(InputStream, FileOutputStream等),我认为文件的情况相同。renameTo或FileUtils.moveFile。

简而言之:

Files.move(source, source.resolveSibling("newname"));

更多的细节:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

以下内容直接复制自http://docs.oracle.com/javase/7/docs/api/index.html:

假设我们想要重命名一个文件为“newname”,保持文件在相同的目录下:

Path source = Paths.get("path/here");
Files.move(source, source.resolveSibling("newname"));

或者,假设我们想要移动一个文件到新的目录,保持相同的文件名,并替换目录中任何现有的该名称的文件:

Path source = Paths.get("from/path");
Path newdir = Paths.get("to/path");
Files.move(source, newdir.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING);

据我所知,重命名文件不会将其内容附加到具有目标名称的现有文件中。

关于在Java中重命名文件,请参阅file类中renameTo()方法的文档。

运行代码在这里。

private static void renameFile(File fileName) {

    FileOutputStream fileOutputStream =null;

    BufferedReader br = null;
    FileReader fr = null;

    String newFileName = "yourNewFileName"

    try {
        fileOutputStream = new FileOutputStream(newFileName);

        fr = new FileReader(fileName);
        br = new BufferedReader(fr);

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            fileOutputStream.write(("\n"+sCurrentLine).getBytes());
        }

        fileOutputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
            if (br != null)
                br.close();

            if (fr != null)
                fr.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}