java代码更改文件名称 修改java文件名
Java能修改文件夹的名字吗
package script;
公司主营业务:网站制作、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出宾阳免费做网站回馈大家。
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("修改失败");
}
}
}
原来写的例子~~~希望能采纳。
java怎么修改文件名称
File file=new File("D:\\abc.java");
if(file.exists())
{
file.renameTo(new File("d:\\123.txt"));
}
java怎样按规定批量修改文件名
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));
}
}
}
}
用java线程实现批量修改文件名
; /** * 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
java修改文件后缀名
以下程序实现的功能是批量修改文件后缀:
import java.io.*;
/**
* JAVA实现的批量更改文件后缀名的程序。
*
* @author rommnn
*/
public class ExtBatchRename {
/**
* 修改程序。br
* 内部递归调用,进行子目录的更名
*
* @param path
* 路径
* @param from
* 原始的后缀名,包括那个(.点)
* @param to
* 改名的后缀,也包括那个(.点)
*/
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");
}
}
名称栏目:java代码更改文件名称 修改java文件名
标题链接:http://myzitong.com/article/hgjsdh.html