* this program TODO
* @version 1.0 2012-9-1
* @ausor widjan wu
*/
package file;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.*;
public class ChangeFileName
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter base directory :”);
String directory = in.nextLine();
System.out.print(“Enter key words:”);
String keywords = in.nextLine();
ExecutorService pool = Executors.newCachedThreadPool();
ChangeName change = new ChangeName(new File(directory), keywords, pool);
Future
try {
System.out.println(result.get() + “files were changed”);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
pool.shutdown();
int largestPoolSize = ((ThreadPoolExecutor) pool)。getLargestPoolSize();
System.out.println(“largest pool size :” + largestPoolSize);
}
}
class ChangeName implements Callable
{
public ChangeName(File directory, String keywords, ExecutorService pool) {
this.directory = directory;
this.pool = pool;
this.keywords = keywords;
}
public Integer call()
{
count = 0;
try
{
File[] files = directory.listFiles();
ArrayList
for (File file : files) {
if (file.isDirectory()) {
ChangeName change = new ChangeName(file, keywords, pool);
Future
} else {
count++;
String path = file.getPath();
int index1 = path.lastIndexOf(“”);
path = path.substring(0, index1+1);
System.out.println(path);
String oldName = file.getName();
String fileType = oldName.substring(oldName.lastIndexOf(“.”));
String newFName = path + keywords + count + fileType;
file.renameTo(new File(newFName));
}
}
for(Future
{
try
{
count +=result.get();
}catch(ExecutionException e)
{
e.printStackTrace();
}
}
}catch(InterruptedException e)
{
}
return count;
}
private File directory;
private String keywords;
private ExecutorService pool;
private int count;
}