Use the following code, all *.txt will be changed
所在版块:社会百科 发贴时间:2003-01-21 12:02  评分:

用户信息
复制本帖HTML代码
高亮: 今天贴 X 昨天贴 X 前天贴 X 
to *.new (tested in UNIX)


import java.io.*;

public class temp{
public static void main(String[] args){
File f = new File(".");
File[] children = f.listFiles();
for (int i=0;i<children.length;i++){
String name = children[i].getName();
if (children[i].getName().endsWith(".txt")){
name = name.replaceAll(".txt",".new");
File f1 = new File(name);
children[i].renameTo(f1);
}
}
}
}


Things to note:
1. Directory is reprensented by class File. I used current directory (.), you can supply absolute path of the directory where you want to rename files.
2. Use listFiles to get all files in that directory.
3. I used some string methods to manipulate the file name.
4. You need to create a new File object and pass it to the method renameTo to change the name.

Hope this can help ....
.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!

俯首仰看娥眉细,何曾折腰觅封侯?
 相关帖子 我要回复↙ ↗回到正文
how to use java program to... hidden   (109 bytes , 476reads )
Use the following code, all *.txt will be changed 天下   (846 bytes , 344reads )
thanks hidden   (0 bytes , 184reads )
check FileSystemView class 逃课专家   (0 bytes , 242reads )
ok, thanks hidden   (0 bytes , 216reads )