More Java posts
October 30, 2002
Renaming files in Java
More file renaming, this time in Java. Demonstrates why you wouldn't really want to do it this way. Sorry about the wacky formatting, I was trying to reduce the line length so it fits on the page better.
import java.io.*;
public class Rename {
public static void main(String[] args) {
File directory =
new File(System.getProperty("user.dir"));
String[] textFileNames =
directory.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
});
for (int i = 0; i < textFileNames.length; ++i) {
File oldFile =
new File(textFileNames[i]);
File newFile =
new File(textFileNames[i].
substring(0,
textFileNames[i].length() - 4) +
".xml");
oldFile.renameTo(newFile);
}
}
}
Posted by Alex at October 30, 2002 09:48 PMComments
Post a comment