how to rename filename by using File API?SOS

szasgmg 2002-10-22 11:14:06
in java.io.File, there is a method named rename(File newFile),
but write some code to execute it, find some problem.code as follows:

import java.io.*;
public class MakeDirectories {
private final static String usage =
"Usage:MakeDirectories path1 ...\n" +
"Creates each path\n" +
"Usage:MakeDirectories -d path1 ...\n" +
"Deletes each path\n" +
"Usage:MakeDirectories -r path1 path2\n" +
"Renames from path1 to path2\n";
private static void usage() {
System.err.println(usage);
System.exit(1);
}
private static void fileData(File f) {
System.out.println(
"Absolute path: " + f.getAbsolutePath() +
"\n Can read: " + f.canRead() +
"\n Can write: " + f.canWrite() +
"\n getName: " + f.getName() +
"\n getParent: " + f.getParent() +
"\n getPath: " + f.getPath() +
"\n length: " + f.length() +
"\n lastModified: " + f.lastModified());
if(f.isFile())
System.out.println("it's a file");
else if(f.isDirectory())
System.out.println("it's a directory");
}
public static void main(String[] args) {
if(args.length < 1) usage();
if(args[0].equals("-r")) {
if(args.length != 3) usage();
File
old = new File(args[1]),
rname = new File(args[2]);
System.out.println(old.renameTo(rname));
fileData(old);
fileData(rname);
return; // Exit main
}
int count = 0;
boolean del = false;
if(args[0].equals("-d")) {
count++;
del = true;
}
for( ; count < args.length; count++) {
File f = new File(args[count]);
if(f.exists()) {
System.out.println(f + " exists");
if(del) {
System.out.println("deleting..." + f);
f.delete();
}
}
else { // Doesn't exist
if(!del) {
f.mkdirs();
System.out.println("created " + f);
}
}
fileData(f);
}
}
}
as execute in command line:f:\>java -r \\f:\temp\hello.txt \\f:\temp\123.txt
it is failed to rename hello.txt as 123.txt
but if i execute: f:\>java -r hello.txt 123.txt
it is succeeded in renaming hello.txt as 123.txt.
i not know where the problem is ?
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuw 2002-10-22
  • 打赏
  • 举报
回复
public boolean renameTo(File dest)
Renames the file denoted by this abstract pathname.
Whether or not this method can move a file from one filesystem to another is platform-dependent. The return value should always be checked to make sure that the rename operation was successful.


Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to either the old or new pathnames
NullPointerException - If parameter dest is null
上面是从javadoc中copy的,你认真看一看renameTo方法对变量的要求!!!!
wjmmml 2002-10-22
  • 打赏
  • 举报
回复
as execute in command line:f:\>java -r \\f:\temp\hello.txt \\f:\temp\123.txt

你好好检查一下语法正确吗???
tpz_stamp 2002-10-22
  • 打赏
  • 举报
回复
去掉 文件里的“\\”,再试试看呢。
xuw 2002-10-22
  • 打赏
  • 举报
回复
你写的有错误,改为下:
f:\>java -r f:/temp/hello.txt f:/temp/123.txt

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧