• 迟到的第14周作业


    一、题目1

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

    二、代码

    Test4.java

    /*
     * 创建一个主类和方法类,手动输入目录 列出所有文件,指定某类型 列出该类型文件
     * 并将文件剪切到其他目录
     * 
     */
    package g;
    
    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 W implements FilenameFilter{
        String lx;
        W(String lx){
            this.lx=lx;
        }
        public boolean accept(File file,String name){
            return name.endsWith(lx);//尾部查询
        }
    }
    public class Test4 {
    
        public static void main(String[] args) {
            Scanner reader=new Scanner(System.in);
            System.out.println("请输入路径:");
            String path=reader.nextLine();
            File file=new File(path);
            String filename[]=file.list();//将所有文件名放入数组里
            for(String name:filename){
                System.out.println(name);
            }
            System.out.println("输入查询后缀:");
            W filew = new W(reader.nextLine());
            String filename1[]=file.list(filew);//将查询后的文件名放入数组里
            for(String name:filename1){
                System.out.println(name);
            }
                 System.out.println("请输入需要剪切的文件名:");
                String cut = reader.nextLine();
                File cutfile = new File(path+"\"+cut);//存入原文件
                System.out.println("请输入该文件需要移动到的目录:");
                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 filedata="";
                Writer writer = null;
                BufferedWriter bufferedWriter = null;
                try {
                    inputStream = new FileInputStream(cutfile);
                    bufferedInputStream = new BufferedInputStream(inputStream);
                    byte[] b = new byte[1024];
                    int count = 0;
                    while((count = bufferedInputStream.read(b, 0, 1024))!=-1){
                        filedata=filedata+new String(b, 0, count);//遍历到字节存入字符串
                    }
                    writer = new FileWriter(newfile);
                    bufferedWriter = new BufferedWriter(writer);
                    bufferedWriter.write(filedata);//字符串写入新文件
                } 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();//删除原文件
        }
    
    }

    三、运行结果

     

     

  • 相关阅读:
    2017年年终总结
    7只老鼠测试100个瓶子
    jgs--多线程和synchronized
    springboot&&vue简单的景点信息管理系统
    springboot&&vue前后端分离入门案例
    npm安装教程
    springboot整合mybatisplus
    spring整合Mybatis-plus
    Springboot简单练手的记账本
    SpringBoot整合thymeleaf简单的CRUD
  • 原文地址:https://www.cnblogs.com/weiyiren666/p/11992117.html
Copyright © 2020-2023  润新知