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

    题目:

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

    一、代码

     1 /**
     2  * @author 李佳琦
     3  */
     4 package ccut;
     5 
     6 import java.io.*;
     7 import java.util.*;
     8 
     9 class Q implements FilenameFilter{
    10     String l;
    11     Q(String l){
    12         this.l=l;
    13     }
    14     public boolean accept(File file,String name){
    15         return name.endsWith(l);
    16     }
    17 }
    18 public class Test {
    19 
    20     public static void main(String[] args) {
    21         Scanner reader=new Scanner(System.in);
    22         System.out.println("请输入路径:");
    23         String path=reader.nextLine();
    24         File file=new File(path);
    25         String filename[]=file.list();
    26         for(String name:filename){
    27             System.out.println(name);
    28         }
    29         System.out.println("输入查询的文件类型:");
    30         Q filew = new Q(reader.nextLine());
    31         String filename1[]=file.list(filew);
    32         for(String name:filename1){
    33             System.out.println(name);
    34         }
    35              System.out.println("请输入剪切的文件名:");
    36             String cut = reader.nextLine();
    37             File cutfile = new File(path+"\"+cut);
    38             System.out.println("请输入该文件需要移动到的目录:");
    39             String cutway = reader.nextLine(); 
    40             File cutlist = new File(cutway); 
    41             File newfile = new File(cutway+"\"+cut);
    42             try {
    43                 newfile.createNewFile();
    44             } catch (IOException e) {
    45                 e.printStackTrace();
    46             }
    47             InputStream inputStream = null;
    48             BufferedInputStream bufferedInputStream = null;
    49             String filedata="";
    50             Writer writer = null;
    51             BufferedWriter bufferedWriter = null;
    52             try {
    53                 inputStream = new FileInputStream(cutfile);
    54                 bufferedInputStream = new BufferedInputStream(inputStream);
    55                 byte[] b = new byte[1024];
    56                 int count = 0;
    57                 while((count = bufferedInputStream.read(b, 0, 1024))!=-1){
    58                     filedata=filedata+new String(b, 0, count);
    59                 }
    60                 writer = new FileWriter(newfile);
    61                 bufferedWriter = new BufferedWriter(writer);
    62                 bufferedWriter.write(filedata);
    63             } catch (FileNotFoundException e1) {
    64                 e1.printStackTrace();
    65             } catch (IOException e) {
    66                 e.printStackTrace();
    67             }finally{
    68                 try {
    69                     bufferedInputStream.close();
    70                     inputStream.close();
    71                     bufferedWriter.close();
    72                     writer.close();
    73                 } catch (IOException e) {
    74                     e.printStackTrace();
    75                 }
    76             }
    77             cutfile.delete();
    78     }
    79 } 

    二、程序运行输出

    原文件目录:

    剪切后文件所在:

  • 相关阅读:
    【python3之文件操作】
    【Python3之字符编码】
    【python3之变量,输入输出,判断,循环】
    【Python3之正则re】
    【Python3的命名空间与作用域,闭包函数,装饰器】
    【Python3的函数初识】
    【Python3的进制扫盲】
    JS实现数组去重方法总结(六种方法)
    localstorage sessionstorage和cookie的区别,以及他们各自的用法总结
    es6 javascript对象方法Object.assign()
  • 原文地址:https://www.cnblogs.com/lietian12345/p/11997146.html
Copyright © 2011-2022 走看看