替换工具3

/**
	 * 保存日志
	 * @throws Exception
	 */
	private static void save(String end) throws Exception
	{
		
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd#HHmmss");
		String nowStr = sdf.format(now);
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < list.size(); i++)
		{
			sb.append(list.get(i) + "\r\n");
		}
		File file = new File("c:/BatchModFile[" + nowStr +"].log");
		OutputStream out = new FileOutputStream(file);
		out.write(sb.toString().getBytes());
		out.write(("\r\n" + end).getBytes());
		out.flush();
		out.close();
	}
	
	/**
	 * 迭代文件夹,找出每个文件
	 * @param path
	 * @param keywords
	 * @throws Exception
	 */
	private static void so(String path, String fromKW, String toKW) throws Exception
	{
		File file = new File(path);
		
		File[] item = file.listFiles();
				
		for(int i = 0; i < item.length; i++)
		{
			if(item[i].isDirectory())
			{
				so(item[i].getPath(), fromKW, toKW);
			}
			else
			{
				readwrite(item[i], fromKW, toKW);
			}
		}
		
	}
	
	/**
	 * 迭代文件夹,找出每个文件夹,修改文件夹 : 因为修改文件夹时文件路径不对,所以要与修改文件分离
	 * @param path
	 * @param keywords
	 * @throws Exception
	 */
	private static void soDir(String path, String fromKW, String toKW) throws Exception
	{
		File file = new File(path);
		
		File[] item = file.listFiles();
				
		for(int i = 0; i < item.length; i++)
		{
			if(item[i].isDirectory())
			{
				soDir(item[i].getPath(), fromKW, toKW);
				
				//如果有RNDIR参数,则替换文件夹名字
				if(inputCmd.get("RNDIR") != null)
				{
					ModDirName(item[i], fromKW, toKW);
				}
			}
		}
		
	}
	
	/**
	 * 修改文件夹名
	 * @param dir
	 * @param fromKW
	 * @param toKW
	 */
	private static void ModDirName(File dir, String fromKW, String toKW)
	{
		/*******修改文件夹名*****/
		String allPath = dir.getPath();
		String[] aps = allPath.split("\\\\");
		String lastDirName = aps[aps.length - 1];
		String newDirName = lastDirName.replace(fromKW, toKW);
		
				
		if(lastDirName.indexOf(fromKW) >= 0)
		{
			String log = "ReName.Dir: " + allPath + " To: " + newDirName;
			list.add(log);
			System.out.println(log);
			
			allPath = allPath.replace(lastDirName, newDirName);
			File newFile = new File(allPath);
			dir.renameTo(newFile);
		}
		/*********************/
	}

你可能感兴趣的:(C++,c,C#)