Java 结合DOS 实现文件剪切

大家都知道File 里面的renameTo 会存在bug,现用Java 调用DOS 实现了这个要求

代码简单,大家随便看看吧,没啥要说的了! 

循环目标文件夹当中以.avi结尾的文件,移动到指定文件夹


package AVIFile;

import java.io.File;
import java.util.ArrayList;

public class FindAVI
{
	public static void main(String[] args) throws Exception
	{
		FindAVI findAVI = new FindAVI();
		FindAVI remove = new FindAVI();

		ArrayList<File> FileLists = (ArrayList<File>) findAVI.findFile();
		for (File FileList : FileLists)
		{
			System.out.println(FileList);
		}

		System.out.println("寻找文件完毕,开始准备移动文件");
		String destination = "D:\\Java Study\\动力节点王勇Java-SSH,DRP视频教程";
		String from = "D:\\12\\1\\1.avi";
		
		remove.removeFile(FileLists, destination, from);
		
	}

	public ArrayList findFile()
	{
		System.out.println("开始寻找复合要求的文件");

		ArrayList<File> arrayList = new ArrayList<File>();

		File f1 = new File("D:\\Java Study\\动力节点王勇Java-SSH,DRP视频教程");

		File[] files = f1.listFiles();

		for (File file : files)
		{
			while (file.isDirectory())
			{
				File[] lastfiles = file.listFiles();

				for (File lastfile : lastfiles)
				{

					if (lastfile.getName().endsWith(".avi"))
					{
						arrayList.add(lastfile);
					}
				}
				break;
			}
		}
		return arrayList;
	}

	public void removeFile(ArrayList<File> fileLists,
			String destinationPath, String fromPath ) throws Exception
	{

		for (int k = 0; k < fileLists.size(); k++)
		{
			String name = fileLists.get(k).getPath();
			String cmdMove = "cmd /c " +  
					"move" + 
							" " +
								'"' +
									name+
								'"' +
							" " +
								'"' +
									destinationPath +
								'"';
			
			System.out.println(cmdMove);
			Process process = Runtime.getRuntime().exec(cmdMove); 
		}

	}

}



你可能感兴趣的:(Java 结合DOS 实现文件剪切)