程序修改文件名

  因为一个文件夹里有许多文件要修改名字,鉴于手动修改太麻烦了,而且严重浪费时间,想偷懒,就想用程序来修改,试了很久,总算搞定了。

  修改前为:国道_东.prj     修改后为:国道_清远.prj

参考代码如下:

方法一:

   File   files=new   File("D:/testFile");   //指定文件名及路径   
        for (String file : files.list()) {        	
			File tempFile = new File(files+"\\"+file);
			String fileName = tempFile.getName();    	
			String fix=fileName.substring(0,fileName.lastIndexOf("_"));
			String suffix=fileName.substring(fileName.lastIndexOf("."));
			File f=new File(files+"\\"+fix+"_清远"+suffix);
			tempFile.renameTo(f); //替换
		
		}

 

方法二:

 File   files=new   File("D:/qingyuan_shape");   //指定文件名及路径   
        for (String file : files.list()) {        	
			File tempFile = new File(files+"\\"+file);
			String fileName = tempFile.getName();
			String temp[]=fileName.split("_");
			String FristName=temp[0];
			String endName=temp[1];
			String suffix=endName.substring(endName.lastIndexOf("."));		
			File f=new File(files+"\\"+FristName+"_清远"+suffix);
	    	               tempFile.renameTo(f); //替换
		
		}

 

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值