零00壹 2021-03-11 11:44 采纳率: 75%
浏览 52
已采纳

关于java.io.File重命名

package cn.mldn.com;

import java.io.File;

public class TestAllRename
{

	public static void main(String[] args) 
	{
		File file=new File("D:"+File.separator+"src");
		long start=System.currentTimeMillis();
		fileRename(file);
		long end=System.currentTimeMillis();
		System.out.println("运行时间"+(end-start));

	}
	public static void fileRename(File file)
	{                                            
		if(file.isDirectory())
		{
			File results[]=file.listFiles();
			if(results!=null)
			{
				for(int x=0;x<results.length;x++)
				{
					if(results[x].isDirectory())
					{
						fileRename(results[x]);
					}
					else
					{
						if(file.isFile())//如果是文件进行重命名
						{
							String fileName=null;
							
							if(file.getName().contains("."))
							{
								fileName=file.getName().substring(0,file.getName().lastIndexOf("."))+".txt";
							}
							else
							{
								fileName=file.getName()+".txt";
							}
							
							File newFile=new File(file.getParentFile(),fileName);//新文件的名称
							file.renameTo(newFile);//重命名
							
						}
						//System.out.println("***");
						
					}
					
					
					
				}
			}
			
		}
	}

}

这段程序,但是文件没有重命名。有人能够解答一下吗?

  • 写回答

1条回答 默认 最新

  • tkzc_shark 2021-03-11 13:57
    关注
    import java.io.File;
    
    
    public class TestAllRename
    {
     
    	public static void main(String[] args) 
    	{
    		File file=new File("D:"+File.separator+"src");
    		long start=System.currentTimeMillis();
    		fileRename(file);
    		long end=System.currentTimeMillis();
    		System.out.println("运行时间"+(end-start));
     
    	}
    	public static void fileRename(File file)
    	{                                            
    		if(file.isDirectory())
    		{
    			File results[]=file.listFiles();
    			if(results!=null)
    			{
    				for(int x=0;x<results.length;x++)
    				{
    					File result = results[x];
    
    					if(result.isDirectory())
    					{
    						fileRename(result);
    					}
    					else
    					{
    						if(result.isFile())//如果是文件进行重命名
    						{
    							String fileName=null;
    							
    							if(result.getName().contains("."))
    							{
    								fileName=result.getName().substring(0,result.getName().lastIndexOf("."))+".txt";
    							}
    							else
    							{
    								fileName=result.getName()+".txt";
    							}
    							
    							File newFile=new File(result.getParentFile(),fileName);//新文件的名称
    							result.renameTo(newFile);//重命名
    							
    						}
    						//System.out.println("***");
    						
    					}
    					
    					
    					
    				}
    			}
    			
    		}
    	}
     
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程