zoukankan      html  css  js  c++  java
  • 第14周作业

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

    源代码:

    TestMain.java
    package a;
    import java.util.Scanner;
    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;
    public class TestMain {
    
        public static void main(String[] args) {
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入原路径:");
            String sourcePath=scanner.nextLine();
            File f=new File(sourcePath);
            FilenameFilter file=new X("sourcePath");
            String[] names=f.list(file);
        for(String name:names) {
            System.out.println(name);
        }
        
        System.out.println("请输入后缀类型:");
        String type=scanner.nextLine();
        X targetType=new X(type);
        String[] targetTypenames=f.list(targetType);
        for(String name:targetTypenames) {
            System.out.println(name);
        }
        System.out.println("请输入要剪切的文件名:");
        String cutName=scanner.nextLine();
        File cutfile=new File(sourcePath+"//"+cutName);
        System.out.println("请输入该文件需要移动到的目标目录:");
        String targetPath=scanner.nextLine();
        File cutlist=new File(targetPath);
        File newfile=new File(targetPath+"//"+cutName);
        try {
            newfile.createNewFile();
        }
        catch(IOException e){
            System.out.println("出现异常");
        }
        
       InputStream in=null;
       BufferedInputStream bis=null;
       Writer w=null;
       BufferedWriter bw=null;
       try {
           in=new FileInputStream(cutfile);
           bis=new BufferedInputStream(in);
           byte[] b=new byte[1024];
           int count=0;
           while((count=bis.read(b,0,1024))!=-1) {
               System.out.println(new String(b));
               
           }
           w=new FileWriter(newfile);
           bw=new BufferedWriter(w);
         //  bw.write(new String(b));
           
       }
       catch(FileNotFoundException e) {
           e.printStackTrace();
       }catch(IOException e) {
           e.printStackTrace();
       }finally {
           try {
               bis.close();
               in.close();
               bw.close();
               w.close();
           }
           catch(IOException e) {
               e.printStackTrace();
           }
       }
       cutfile.delete();
        }
    
    }
    class X implements FilenameFilter{
        String type;
        X(String type){
            this.type=type;
        }
        public boolean accept (File f,String name) {
            return name.endsWith(type);
        }
    }

    运行结果:

  • 相关阅读:
    Oracle常用命令
    Infobright 数据仓库
    ORACLE RMAN备份及还原
    将数据库转换为归档日志模式
    Oracle SCN详解
    Android中Intent与Bundle 在传值时有什么不同
    如何让自定义Dialog上下居中
    activity中onResume()的用处
    TabHost 两种使用方法 直接让一个Activity 继承TabActivity 和 利用findViwById()方法取得TagHost组件
    豌豆荚进程与adb端口冲突
  • 原文地址:https://www.cnblogs.com/GXTSTAY/p/12000779.html
Copyright © 2011-2022 走看看