迟到的第14周作业
-
题目
编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件。之后,将这些文件中的某一个文件剪切到另外一个目录中。
-
源程序
Test.java
1 /** 2 * 文件功能:查找指定文件夹的指定类型文件,再选择一个文件,将它剪切到指定的文件夹 3 */ 4 import java.util.*; 5 import java.io.*; 6 7 public class Test { 8 9 public static void main(String[] args) { 10 Scanner in = new Scanner(System.in); 11 System.out.println("请输入路径:"); 12 String path = in.next();// 路径 13 System.out.println("请输入类型:"); 14 String type = in.next();// 文件类型 15 16 FileTypeSearch s = new FileTypeSearch(path, "."+type);//没有"."会匹配文件夹 17 18 if (s.Search()) { //调用这个方法查找指定的文件类型,如果有,会继续执行,没有则结束 19 System.out.println("选择一个文件:"); 20 String filename = in.next();// 文件名 21 System.out.println("选择要剪切到的路径:"); 22 String topath = in.next();// 路径 23 Cut cut = new Cut(path, filename, topath); 24 cut.cut(); 25 } 26 27 } 28 29 }
x.java
1 /** 2 * 文件功能:实现FilenameFilter接口 3 * 方法: 构造方法x(String type)为成员变量赋值 4 * public boolean accept(File dir, String name)对比传入的串(文件名)后缀 5 * 是否以传入的"."type修饰 6 */ 7 import java.io.File; 8 import java.io.FilenameFilter; 9 10 class x implements FilenameFilter { 11 String type; 12 13 x(String type) { 14 this.type = type; 15 } 16 17 public boolean accept(File dir, String name) { 18 return name.endsWith(type); 19 } 20 21 }
FileTypeSearch.java
1 /** 2 * 文件功能:查找指定文件夹的指定类型文件 3 * 成员变量:path文件夹路径 type文件类型 4 * 方法: 构造方法FileTypeSearch(String path, String type) 为成员变量赋值 5 * public boolean Search() 实现文件功能 6 */ 7 import java.io.File; 8 import java.io.FilenameFilter; 9 10 public class FileTypeSearch { 11 String path; 12 String type; 13 14 FileTypeSearch(String path, String type) { 15 this.path = path; 16 this.type = type; 17 } 18 19 public boolean Search() { 20 21 File file = new File(path); 22 FilenameFilter obj = new x(type); 23 24 String[] dir = file.list(obj); 25 26 if (dir.length < 1) { 27 System.out.println("什么都没有呀~OwO"); 28 return false; 29 } 30 31 for (String name : dir) { 32 System.out.println(name); 33 } 34 return true; 35 } 36 }
Cut.java
1 /** 2 * 文件功能:将指定路径的文件剪切到指定的路径 3 * 成员变量:path源文件路径 filename需要剪切的文件名 topath剪切目标路径 4 * 方法: 构造方法Cut(String path, String filename, String topath)为成员变量赋值 5 * public boolean cut()实现文件功能 6 */ 7 import java.io.*; 8 9 public class Cut { 10 11 String path; 12 String filename; 13 String topath; 14 15 Cut(String path, String filename, String topath) { 16 17 this.path = path; 18 this.filename = filename; 19 this.topath = topath; 20 21 } 22 23 public boolean cut() { 24 path+="\\"; 25 File file1 = new File(path +filename); 26 if(file1.exists()) { 27 28 } else { 29 System.out.println("没有这个文件哦~OwO"); 30 return false; 31 } 32 33 File file2 = new File(topath + filename); 34 FileInputStream fis = null; 35 BufferedInputStream in = null; 36 FileOutputStream out = null; 37 38 try { 39 40 fis = new FileInputStream(file1); 41 in = new BufferedInputStream(fis); 42 out = new FileOutputStream(file2, true); 43 44 byte[] b = new byte[1024]; 45 int count = 0; 46 47 while ((count = in.read(b, 0, 1024)) != -1) { 48 out.write(b); 49 } 50 51 } catch (FileNotFoundException e) { 52 e.printStackTrace(); 53 54 } catch (IOException e) { 55 e.printStackTrace(); 56 57 } finally { 58 try { 59 fis.close(); 60 in.close(); 61 out.close(); 62 } catch (IOException e) { 63 e.printStackTrace(); 64 } finally { 65 if(file2.exists()) { 66 System.out.println("剪切成功~OwO"); 67 } 68 file1.delete();// 删除原文件 69 } 70 } 71 return true; 72 } 73 }
-
运行结果




-
如果没有"."


浙公网安备 33010602011771号