Z形记之比较两个目录下文件异同

比较两个目录下文件的异同

前几天部门客服组的同事让我们帮着转一些视频,主要是HD和WEB两种格式的,分别有3378个,最主要的是每个视频的文件名是64位自动生成的,这要是其中转重了或者少转了,找起来相当麻烦,所有我就用程序去跑了一遍,效果还行。

转视频

1.先说说思路

a.先根据目录路径找到目录下的所有(.mp4)结尾的文件,并将这些文件放到一个List中
b.将两个List进行循环比较,找出其中不同的文件,并将之记录输出

2.代码实现

public static void main(String[] args) 
{
	List<String> list1 = getName("d:\Video\hd");
	List<String> list2 = getName("d:\Video\web");
	
	for( int i = 0; i < list1.size(); i++)
	{
		//在java中"."在分隔时必须要加"\"
		String str1 = list1.get(i).split("\.")[0];
		for ( int j = 0; j < list2.size(); j++)
		{
			String str2 = list2.get(j).split("\.")[0];
			if ( !str2.equals(str1) )
			{
				System.out.println("HD高清" + str1 + "不同WEB" + str2);
			}
		}
	}
	
}

public static List<String> getName(String path)
{
	File file = new File(path);
	List<String> list = new ArrayList<String>(); 
	if(file.isDirectory())
	{
		File[] fileList = file.listFiles();
		if( fileList != null)
		{
			for(File f : fileList)
			{
				if(f.isDirectory())
				{
					getName(f.getAbsolutePath());
				}
				else
				{
					//找到以.mp4结尾的文件
					if(f.getName().endsWith(".mp4"))
					{
						//getAbsolutePath()返回抽象路径名的绝对路径名字符串,说起来有点拗口,其实就是一个绝对路径名
						String spath = f.getAbsolutePath();
						//这里用来区分传入的路径
						if(f.getPath().contains("d:\Video\hd"))
						{
							spath = spath.substring(12);
						}
						else
						{
							spath = spath.substring(13);
						}
						list.add(spath);
					}
				}
			}
		}
		
	}
	return list;
}
原文地址:https://www.cnblogs.com/alarm1673/p/5021013.html