• 迟到的第14周作业


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

    ps:第一次课请先完成前一部分

    import java.io.File;
    import java.io.FilenameFilter;
    import java.util.Scanner;
    
    	class type implements FilenameFilter{//创建FilenameFilter接口子类
    	    String str = null;
    	    type(String str){//定义构造方法
    	        this.str = "."+str;
    	    }
    	    public boolean accept(File dir, String name) {//重写accept方法
    	        return name.endsWith(str);//文件类型
    	    }
    	}
    	public class text {
    	public static void main(String[] args) {
    		Scanner reader = new Scanner(System.in);
    	    System.out.print("请输入目录
    ");
    	    File file = new File(reader.nextLine());//获取输入的目录
    	    String[] all = file.list();//调用无参list方法获取全部文件名
    	    System.out.print("该目录下文件为:
    ");
    	    for(int i = 0;i<all.length;i++){//输出
    	    	
    	    	System.out.print(all[i]+"
    ");	       
    	    }	       
    	    System.out.print("请输入该目录下要求的文件类型
    ");	        
    	    type filetype = new type(reader.nextLine());    
    	    String[] Type = file.list(filetype);//获取文件名	      
    	    System.out.print("该目录下该文件类型的文件为:
    ");	        
    	    for(int i = 0;i<Type.length;i++){          
    	    	System.out.print(Type[i]+"
    ");
    	        }
    	    }
    	}
    

      运行界面

    import java.io.BufferedInputStream;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.FilenameFilter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Writer;
    import java.util.Scanner;
    
    class type implements FilenameFilter{//创建接口子类
        String str = null;
        type(String str){//定义构造方法
            this.str = "."+str;
        }
        public boolean accept(File dir, String name) {//重写accept方法
            return name.endsWith(str);
        }
    }
    
    public class cut {
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            System.out.print("请输入目录:
    ");
            String way = reader.nextLine();//暂存目录
            File file = new File(way);//获取输入的目录
            String[] all = file.list();//list方法获取全部文件名
            System.out.print("该目录下文件为:
    ");
            for(int i = 0;i<all.length;i++){//输出
                System.out.print(all[i]+"
    ");
            }
            System.out.print("请输入该目录下要求的文件类型:
    ");
            type filetype = new type(reader.nextLine());
            String[] Type = file.list(filetype);//list方法获取该类文件名
            System.out.print("该目录下该文件类型的文件为:
    ");
            for(int i = 0;i<Type.length;i++){
                System.out.print(Type[i]+"
    ");
            }
            System.out.print("请输入需要剪切的文件名:
    ");
            String cut = reader.nextLine();//暂存文件名
            File cutfile = new File(way+"\"+cut);//存入原文件
            System.out.print("请输入该文件需要移动到的目录:
    ");
            String cutway = reader.nextLine(); //暂存目录
            File cutlist = new File(cutway);    
            File newfile = new File(cutway+"\"+cut);//存入新目录下新文件
            try {
                newfile.createNewFile();//创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
            InputStream inputStream = null;//读出原文件内容对象创建
            BufferedInputStream bufferedInputStream = null;
            String data="";//内容暂存器
            Writer writer = null;//对象创建
            BufferedWriter bufferedWriter = null;
            try {
                inputStream = new FileInputStream(cutfile);
                bufferedInputStream = new BufferedInputStream(inputStream);
                byte[] content = new byte[1024];//存储内容
                int count = 0;//记录长度
                while((count = bufferedInputStream.read(content, 0, 1024))!=-1){
                    data=data+new String(content, 0, count);//字节存入字符串
                }
                writer = new FileWriter(newfile);
                bufferedWriter = new BufferedWriter(writer);
                bufferedWriter.write(data);//字符串写入新文件
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    bufferedInputStream.close();//关闭流
                    inputStream.close();
                    bufferedWriter.close();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            cutfile.delete();//删除原文件
        }
    }

  • 相关阅读:
    LeetCode 34. Find First and Last Position of Element in Sorted Array
    LeetCode 875. Koko Eating Bananas
    LeetCode 560. Subarray Sum Equals K
    Vue 3 响应式原理源码解析 All In One
    git rebase 与 git merge 的区别是什么 All In One
    superscript & subscript symbol All In One
    macOS & ifconfig & ipconfig commands All In One
    Chrome UX Report All In One
    TypeScript Utility Types All In One
    TypeScript Type Manipulation All In One
  • 原文地址:https://www.cnblogs.com/mai98/p/12006704.html
Copyright © 2020-2023  润新知