package?script;
公司主營業(yè)務(wù):網(wǎng)站制作、做網(wǎng)站、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出賓陽免費(fèi)做網(wǎng)站回饋大家。
import?java.io.File;
import?java.io.IOException;
public?class?Realname?{
public?static?void?main(String[]?args)?throws?IOException?
{
File?oldFile?=?new?File("d:/PMS");
if(!oldFile.exists())
{
oldFile.createNewFile();
}
System.out.println("修改前文件名稱是:"+oldFile.getName());
String?rootPath?=?oldFile.getParent();
System.out.println("根路徑是:"+rootPath);
File?newFile?=?new?File(rootPath?+?File.separator?+?"PMSTmp");
System.out.println("修改后文件名稱是:"+newFile.getName());
if?(oldFile.renameTo(newFile))?
{
System.out.println("修改成功!");
}?
else?
{
System.out.println("修改失敗");
}
}
}
原來寫的例子~~~希望能采納。
File file=new File("D:\\abc.java");
if(file.exists())
{
file.renameTo(new File("d:\\123.txt"));
}
java批量修改文件名:
public static void main(String[] args) {
updateFileNames("D:\\jjjj", "第");
}
public static void updateFileNames(String url, String index){
File file = new File(url);
//判斷文件目錄是否存在,且是文件目錄,非文件
if(file.exists() file.isDirectory()){
File[] childFiles = file.listFiles();
String path = file.getAbsolutePath();
for(File childFile : childFiles){
//如果是文件
if(childFile.isFile()){
String oldName = childFile.getName();
String newName = oldName.substring(oldName.indexOf(index));
childFile.renameTo(new File(path + "\\" + newName));
}
}
}
}
;?? /**??? ??? * this program TODO??? ??? * @version ??? ??? * @ausor widjan wu??? ??? */??? ??? package file;??? ??? import java io File;??? ??? import java util ArrayList;??? ??? import java util Scanner;??? ??? import ncurrent *;??? ??? 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) ??? ??? FutureInteger result = pool submit(change) ??? ??? 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 CallableInteger??? ??? {??? ??? public ChangeName(File directory String keywords ExecutorService pool) {??? ??? this directory = directory;??? ??? this pool = pool;??? ??? this keywords = keywords;??? ??? }??? ??? public Integer call()??? ??? {??? ??? count = ;??? ??? try??? ??? {??? ??? File[] files = directory listFiles() ??? ??? ArrayListFutureInteger》 results = new ArrayListFutureInteger》() ??? ??? for (File file : files) {??? ??? if (file isDirectory()) {??? ??? ChangeName change = new ChangeName(file keywords pool) ??? ??? FutureInteger result = pool submit(change) ??? ??? } else {??? ??? count++;??? ??? String path = file getPath() ??? ??? int index = path lastIndexOf( \\ ) ??? ??? path = path substring( index + ) ??? ??? 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(FutureInteger result:results)??? ??? {??? ??? 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;??? ??? } lishixinzhi/Article/program/Java/gj/201311/27511
以下程序?qū)崿F(xiàn)的功能是批量修改文件后綴:
import java.io.*;
/**
* JAVA實(shí)現(xiàn)的批量更改文件后綴名的程序。
*
* @author rommnn
*/
public class ExtBatchRename {
/**
* 修改程序。br
* 內(nèi)部遞歸調(diào)用,進(jìn)行子目錄的更名
*
* @param path
* 路徑
* @param from
* 原始的后綴名,包括那個(gè)(.點(diǎn))
* @param to
* 改名的后綴,也包括那個(gè)(.點(diǎn))
*/
public void reName(String path, String from, String to) {
File f = new File(path);
File[] fs = f.listFiles();
for (int i = 0; i fs.length; ++i) {
File f2 = fs[i];
if (f2.isDirectory()) {
reName(f2.getPath(), from, to);
} else {
String name = f2.getName();
if (name.endsWith(from)) {
f2.renameTo(new File(f2.getParent() + "/" + name.substring(0, name.indexOf(from)) + to));
}
}
}
}
public static void main(String[] args) {
ExtBatchRename rf = new ExtBatchRename();
rf.reName("d:/", ".jsp", ".html");
}
}