如何在 Java 7 中重命名(而不是移动)文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1914474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:50:35  来源:igfitidea点击:

How do I rename (not move) a file in Java 7?

javafile-iopathniojava-7

提问by java.is.for.desktop

I'm a bit confused with all these new File I/O classes in JDK7.

我对 JDK7 中所有这些新的 File I/O 类有点困惑。

Let's say, I have a Pathand want to rename the file it represents. How do I specify the new name, when again a Pathis expected?

比方说,我有一个Path并想重命名它代表的文件。当再次需要 a 时,如何指定新名称Path

Path p = /* path to /home/me/file123 */;
Path name = p.getName(); /* gives me file123 */
name.moveTo(/* what now? */); /* how to rename file123 to file456? */

NOTE: Why do I need JDK7? Handling of symbolic links!

注意:为什么我需要JDK7符号链接的处理!

Problem is: I have to do it with files whose names and locations are known at runtime. So, what I need, is a safemethod (without exceptional side-effects) to create a new name-Path of some old name-Path.

问题是:我必须使用名称和位置在运行时已知的文件来执行此操作。所以,我需要的是一种安全的方法(没有特殊的副作用)来创建一些旧名称路径的新名称路径。

Path newName(Path oldName, String newNameString){
    /* magic */ 
}

采纳答案by Alan

You have a path string and you need to create a Path instance. You can do this with the getPath method or resolve. Here's one way:

您有一个路径字符串,您需要创建一个 Path 实例。您可以使用 getPath 方法或解析来执行此操作。这是一种方法:

Path dir = oldFile.getParent();        
Path fn = oldFile.getFileSystem().getPath(newNameString);
Path target = (dir == null) ? fn : dir.resolve(fn);        
oldFile.moveTo(target); 

Note that it checks if parent is null (looks like your solution don't do that).

请注意,它会检查 parent 是否为空(看起来您的解决方案没有这样做)。

回答by pavium

If the destination path is identical to the source path except for the name of the file, it will be renamed rather than moved.

如果目标路径与源路径相同,除了文件名,它将被重命名而不是移动。

So for your example, the moveto path should be

所以对于你的例子,移动路径应该是

/home/me/file456

回答by Brian Agnew

If you take a look at Apache Commons IOthere's a class called FileNameUtils. This does a ton of stuff wrt. file path names and will (amongst other things) reliably split up path names etc. I think that should get you a long way towards what you want.

如果您查看Apache Commons IO,则有一个名为FileNameUtils的类。这做了很多东西。文件路径名并且将(除其他外)可靠地拆分路径名等。我认为这应该会让你朝着你想要的方向走很长一段路。

回答by java.is.for.desktop

OK, after trying everything out, it seems I found the right method:

好的,在尝试了一切之后,我似乎找到了正确的方法:

// my helper method
Path newName(Path oldFile, String newNameString){
    // the magic is done by Path.resolve(...)
    return oldFile.getParent().resolve(newNameString);
}

// so, renaming is done by:
oldPath.moveTo(newName(oldFile, "newName"));

回答by Glen P

If you can't get Java to do what you want with Unix I recommend Python scripts (run by your Java program). Python has get support for Unix scripting and it's not Perl :) This might sound inelegant to you but really in a larger program you'll benefit from using the right tool for the job.

如果你不能让 Java 用 Unix 做你想做的事,我推荐 Python 脚本(由你的 Java 程序运行)。Python 已经获得了对 Unix 脚本的支持,它不是 Perl :) 这对你来说可能听起来不雅,但实际上在一个更大的程序中,你会受益于使用正确的工具来完成这项工作。

回答by Avner

In JDK7, Files.move()provides a short and concise syntax for renaming files:

在 JDK7 中,Files.move()为重命名文件提供了简洁的语法:

Path newName(Path oldName, String newNameString) {
    return Files.move(oldName, oldName.resolveSibling(newNameString));
}

First we're getting the Path to the new file name using Path.resolveSibling()and the we use Files.move()to do the actual renaming.

首先,我们使用Path.resolveSibling()获取新文件名的路径, 然后使用Files.move()进行实际重命名。