第14周作业

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

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

代码部分:

Text类:两个对象

/**
 *声明Content类对象co,用构造方法Content(String s1,String w1)进行实例化
 *声明cut类对象cu,用构造方法cut(String s1,String w1)进行实例化
 *字符串s1位原文件夹地址
 *字符串w1为文件后缀名
 */

package cn.edu.ccut.Fourteen;
import java.io.*;
import java.util.Scanner;
class FileAccept implements FilenameFilter{
    String str =null;
    FileAccept(String s){
        str ="."+s;
    }
    public boolean accept(File dir,String name) {
        return name.endsWith(str);
   }
}
public  class Test {
    public static void main(String[] args) {
        Scanner der=new Scanner(System.in);
        System.out.println("输入目录(例:C:\java\)");
        String s1=der.nextLine();//字符串s1位原文件夹地址
        System.out.println("输入文件的后缀名(例:java)");
        String w1=der.nextLine();//字符串w1为文件后缀名
        Content co=new Content(s1,w1);//声明Content类对象co,用构造方法Content(String s1,String w1)进行实例化
        co.Content();
        cut cu=new cut(co.s1,w1);//声明cut类对象cu,用构造方法cut(String s1,String w1)进行实例化
    }
    
}

cut类:两个对象;一个构造方法

/**
 * 字符串s3为需要剪切的文件名
 * 字符串s2为新文件的绝对路径
 * cz为新文件的对象
 * f2位原文件的对象
 */

package cn.edu.ccut.Fourteen;

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

public class cut {
    public cut(String s1,String w1) {
        Scanner der=new Scanner(System.in);
        System.out.println("输入需要剪切的文件名");
        String s3=der.nextLine();//s3需要剪切的文件名
        System.out.println("输入导入的文件夹");
        String s2=der.nextLine()+s3;//新文件的绝对路径
        File cz=new File(s2);//生成新文件的对象
        
        File f2=new File(s1+s3);//原文件的对象
        
        InputStream in =null;
        BufferedInputStream bin =null;//整行字节输入流
        Writer w=null;//字符输出流基类
        BufferedWriter bw=null;//整行字符输出流对象
        try {
            
            in =new FileInputStream(f2);
            bin=new BufferedInputStream(in);
            byte[] b=new byte[1024]; 
            int count=0;
            w=new FileWriter(cz,true);
            bw=new BufferedWriter(w);
            while ((count = bin.read(b,0,1024))!=-1) {
                bw.write(new String(b,0,count));
            }
            f2.delete();//删除原文件
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        try {
                
            bin.close();
            in.close();
            bw.close();
            w.close();
    
        }catch(IOException e) {
            e.printStackTrace();
            }finally {
                System.out.println("操作结束");
            }
        }        
    }
    
}

Content类:两个成员变量。两个构造方法

package cn.edu.ccut.Fourteen;

import java.io.File;
import java.util.Scanner;

public class Content {
    String s1;//文件夹路径
    String w1;//后缀
    public Content(String s1,String w1) {
        this.s1=s1;
        this.w1=w1;
    }
    public void Content() {
        Scanner der=new Scanner(System.in);
        
        File f=new File(s1);
        FileAccept acceptCondition =new FileAccept(w1);
        String fileList[] =f.list(acceptCondition);
        System.out.println("目录下有"+fileList.length+"个文件");
    for (int i=0;i<fileList.length;i++) {
        System.out.println(fileList[i]);
    }
}
}

运行截图:

剪切前:

剪切后:

原文地址:https://www.cnblogs.com/sunshuaiqun/p/11985575.html