第14周作业

题目:编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。

显示类

package wenjianzuoye;

import java.io.*;

public class x implements FilenameFilter {
	String type;

	x(String type) {
		this.type = type;
	}

	public boolean accept(File file, String name) {
		return name.endsWith(type);
	}
}

 剪切类

 

package wenjianzuoye;

import java.io.*;

public class jianqie {


		public boolean jianqie(File f1, File f2) throws IOException {
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));
			int len = 0;
			byte[] b = new byte[1024];
			while ((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			bis.close();
			bos.close();
			fileDelete(f1);
			return true;
		}

		public void fileDelete(File f) {
			if (f.isFile()) {
				f.delete();
			} else {
				File[] arr = f.listFiles();
				for (int i = 0; i < arr.length; i++) {
					fileDelete(arr[i]);
				}
				f.delete();
			}
		}
	}

 

  主类

package wenjianzuoye;

import java.io.*;
import java.util.*;

public class zuoye {

	public static void main(String[] args) throws IOException {
		Scanner sca = new Scanner(System.in);
		System.out.println("请输入文件目录");
		String path = sca.next();
		File dir = new File(path);
		System.out.println("请输入文件类型");
		String T = sca.next();
		FilenameFilter f = new x(T);
		String[] names = dir.list(f);
		for (String name : names) {
			System.out.println(name);
		}
		 System.out.println("请输入要移动到的目录:");
	        String cutPath = sca.next();
	        jianqie cut = new jianqie();           
	        boolean isCut = cut.jianqie(new File(path + "//" + names[0]), new File(cutPath + "//" + names[0]));
	        if (isCut) {
	            System.out.println("文件已经剪切到" + cutPath);
	        }
	}

}

  

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-08 15:33  l刘磊  阅读(126)  评论(0编辑  收藏  举报