迟到的第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("请输入目录\n");
	    File file = new File(reader.nextLine());//获取输入的目录
	    String[] all = file.list();//调用无参list方法获取全部文件名
	    System.out.print("该目录下文件为:\n");
	    for(int i = 0;i<all.length;i++){//输出
	    	
	    	System.out.print(all[i]+"\n");	       
	    }	       
	    System.out.print("请输入该目录下要求的文件类型\n");	        
	    type filetype = new type(reader.nextLine());    
	    String[] Type = file.list(filetype);//获取文件名	      
	    System.out.print("该目录下该文件类型的文件为:\n");	        
	    for(int i = 0;i<Type.length;i++){          
	    	System.out.print(Type[i]+"\n");
	        }
	    }
	}

  运行界面

 

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("请输入目录:\n");
        String way = reader.nextLine();//暂存目录
        File file = new File(way);//获取输入的目录
        String[] all = file.list();//list方法获取全部文件名
        System.out.print("该目录下文件为:\n");
        for(int i = 0;i<all.length;i++){//输出
            System.out.print(all[i]+"\n");
        }
        System.out.print("请输入该目录下要求的文件类型:\n");
        type filetype = new type(reader.nextLine());
        String[] Type = file.list(filetype);//list方法获取该类文件名
        System.out.print("该目录下该文件类型的文件为:\n");
        for(int i = 0;i<Type.length;i++){
            System.out.print(Type[i]+"\n");
        }
        System.out.print("请输入需要剪切的文件名:\n");
        String cut = reader.nextLine();//暂存文件名
        File cutfile = new File(way+"\\"+cut);//存入原文件
        System.out.print("请输入该文件需要移动到的目录:\n");
        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();//删除原文件
    }
}

 

posted @ 2019-12-08 18:57  麦麦哈  阅读(95)  评论(0编辑  收藏  举报