zoukankan      html  css  js  c++  java
  • 迟来的作业

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

    package zuoyeshiyi;
    import java.io.*;
    import java.util.*;
    public class fileDemo {
     
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sca=new Scanner(System.in);
            System.out.println("请输入需要显示的目录");
            String str=sca.nextLine();
            System.out.println("输入需要过滤的文件类型");
            String str1=sca.nextLine();
            File f=new File(str);
                System.out.println("后缀名为"+str1+"的文件有");
                x(f,str1);
                System.out.println("请输入需要剪切的文件");
                String str2=sca.nextLine();
                System.out.println("请输入剪切到的文件名称");
                String str3=sca.nextLine();
                jianqie(str2,str3);
                File file=new File(str2);
                file.delete();
        }
        public static void x(File f,String str1) {
            String []arr=f.list(new FilenameFilter() {
                public boolean accept(File dir,String name)
                {  
                    return name.endsWith(str1);//后缀名是否相同
                }
            });
            System.out.println(arr.length);
            for(String name:arr) {
                System.out.println(name);
            }
        }
        public static void jianqie(String str2,String str3)  {
             
            BufferedInputStream bis=null;
            BufferedOutputStream bos=null;
            try {
                 bis=new BufferedInputStream(new FileInputStream(str2));
                 bos=new BufferedOutputStream(new FileOutputStream(str3));
                int k=0;
                while((k=bis.read())!=-1) {
                    bos.write(k);
                    bos.flush();               
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }  
        }
    }

     

  • 相关阅读:
    模拟最烂的网速
    TableView编辑状态下跳转页面的崩溃处理
    Swift的Optional类型
    autolayout之后获取uiview的frame
    Swift中的闭包(Closure)[转]
    Swift1.2与Xcode6.3 beta
    python技巧31[python中使用enum][转]
    Python初学者的捷径[译]
    tornado+bootstrap急速搭建你自己的网站
    Windows下nginx+tomcat实现简单的负载均衡、动静分离等
  • 原文地址:https://www.cnblogs.com/chen4635/p/12008458.html
Copyright © 2011-2022 走看看