zoukankan      html  css  js  c++  java
  • 第14周作业

     

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

    package com;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Scanner;
    
    public class A {
    	public static void main(String[] args) {
    		Scanner reader = new Scanner(System.in);
    		System.out.print("请输入目录");
    		String path = reader.nextLine();
    		File f = new File(path);
    		String[] filenames = f.list();
    		System.out.print("该目录的文件为" + "
    ");
    
    		for (int i = 0; i < filenames.length; i++) {
    			System.out.print(filenames[i] + "
    ");
    		}
    		System.out.print("请输入你想查找文件的类型:");
    		X1 typefile = new X1(reader.nextLine());
    		String[] filenames1 = f.list(typefile);
    		System.out.print("此类型文件为");
    		for (int i = 0; i < filenames1.length; i++) {
    			System.out.print(filenames1[i] + "
    ");
    		}
    		System.out.print("请输入需要剪切的文件名:
    ");
    		String cut = reader.nextLine();
    		File cutfile = new File(path + "\" + cut);
    		System.out.print("请输入该文件需要移动到的目录:
    ");
    		String cutpath = reader.nextLine();
    
    		File file1 = new File(cutpath);
    		file1 = cutfile;
    		File file2 = new File(cutpath + "\" + cut);
    
    		// 在程序结束时删除文件1
    		file1.deleteOnExit();
    		try {
    
    			file2.createNewFile();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		cutFile(file1, file2);
    	}
    
    	public static void cutFile(File file1, File file2) {
    		FileOutputStream fileOutputStream = null;
    		InputStream inputStream = null;
    		byte[] bytes = new byte[1024];
    		int temp = 0;
    		try {
    			inputStream = new FileInputStream(file1);
    			fileOutputStream = new FileOutputStream(file2);
    			while ((temp = inputStream.read(bytes)) != -1) {
    				fileOutputStream.write(bytes, 0, temp);
    				fileOutputStream.flush();
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (inputStream != null) {
    				try {
    					inputStream.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    			if (fileOutputStream != null) {
    				try {
    					fileOutputStream.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    
    	}
    }
    
    class X1 implements FilenameFilter {
    	String type;
    
    	X1(String type) {
    		this.type = type;
    	}
    
    	public boolean accept(File file, String name) {
    		return name.endsWith(type);
    	}
    }
    

      

     

     

  • 相关阅读:
    【洛谷4251】 [SCOI2015]小凸玩矩阵(二分答案,二分图匹配)
    JXOI2019游记
    luogu4884 多少个1?
    数论难点选讲
    计树问题小结
    codeforces选做1.0
    POI2015选做
    后缀自动机小结
    bzoj4008 [HNOI2015]亚瑟王
    bzoj1500 [NOI2005]维修数列
  • 原文地址:https://www.cnblogs.com/hzcxwz/p/12007741.html
Copyright © 2011-2022 走看看