• 迟到的第14周作业


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

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

    二、代码

    1.Test

    /**
     * 创建FileAccept包含
     * 成员变量str
     * 构造方法
     * accept方法
     * 创建Test类
     * 包含主方法
     * FileAccept类的对象acceptCondition
     * 调用fileList方法
     */
    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.util.Scanner;
    class FileAccept implements FilenameFilter{
        String str = null;
        FileAccept(String s){
            str = "."+s;
        }
        @Override
        public boolean accept(File dir, String name) {
            // TODO Auto-generated method stub
            return name.endsWith(str);
        }
    }
    public class Test {
        public static void main(String[] args) throws IOException {
            Scanner reader=new Scanner(System.in);
            System.out.println("请输入文件路径:");
            String P =reader.nextLine();
            File dir = new File(P);
            System.out.println("请输入需要查找的文件后缀");
            String A =reader.nextLine();
            FileAccept acceptCondition = new  FileAccept(A);//设置文件名后缀
            String fileList[] =dir.list(acceptCondition);   //获取文件名
                System.out.println("目录下有"+fileList.length+"文件");
            for (int i = 0; i < fileList.length; i++) {
                System.out.println(fileList[i]);
            }
            System.out.println("请输入需要剪切的文件名:");
            String name = reader.nextLine();
            System.out.println("请输入需要剪切至那个目录");
            String target = reader.nextLine();
            File namefile = new File(P+"\"+name);
            FileInputStream in = new FileInputStream(namefile);   
            File targetfile = new File(target+"\"+name);
            targetfile.createNewFile();           //在目标目录创建新文件;
            FileOutputStream out =  new FileOutputStream(targetfile);
            byte[] bytes = new byte[1024];
            int count = 0 ;
            try {
                while((count = in.read(bytes, 0, 1024)) != -1){    //将原文件内容读写入到目标文件中
                    out.write(bytes, 0, count);
                    out.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }finally{           
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(out != null)
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
            namefile.delete(); //删除原文件
        }
    }
       
     
     

    三、运行结果

  • 相关阅读:
    android listview 添加数据
    Android 打开其他程序
    Android 自定义ListView Item侧滑删除
    Intent 传递对象
    openfire Android学习(六)----总结
    openfire Android学习(五)------连接断开重连
    openfire Android 学习(四)----单人聊天和多人聊天(发送消息、接收消息)
    openfire Android学习(三)----会议室创建、加入以及查询会议室中所有成员等
    openfire Android学习(二)----对分组、好友和头像等一些操作
    openfire Android学习(一)----实现用户注册、登录、修改密码和注销等
  • 原文地址:https://www.cnblogs.com/Xwwg/p/12000972.html
Copyright © 2020-2023  润新知