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

    一、题目

    编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件

    二、源程序

    Test.java

    package pac_6;
    
    import java.io.*;
    
    public class Test {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            File dir=new File("f:\Music");
            ListFile lf=new ListFile("flac");
            String list[]=dir.list(lf);
            for(String sss:list){
                System.out.println(sss);
            }
            
        }
    }
    class ListFile implements FilenameFilter{
        String str=null;
        ListFile(String s){
            str="."+s;
        }
        @Override
        public boolean accept(File dir,String name) {
            return name.endsWith(str);
        }
    }

    三、运行结果

     

    一、题目

    之后,将这些文件中的某一个文件剪切到另外一个目录中。

    二、源程序

    CutFileTest.java

    package pac_6;
    
    import java.io.*;
    
    public class CutFileTest {
        public static void main(String[] args) {
            File file1 = new File("f:\Music\周杰伦 - 夜曲.flac");
            File file2 = new File("e:\周杰伦 - 夜曲.flac");
            file1.deleteOnExit();
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            cutFile(file1, file2);
        }
        public static void cutFile(File file1, File file2){
            FileOutputStream fileOutputStream = null;
            InputStream inputStream = null;
            byte[] bytes = new byte[1024];
            int temp = 0;
            try {
                inputStream = new FileInputStream(file1);
                fileOutputStream = new FileOutputStream(file2);
                while((temp = inputStream.read(bytes)) != -1){
                    fileOutputStream.write(bytes, 0, temp);
                    fileOutputStream.flush();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }finally{
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            
        }
    
    }    

    三、运行结果

  • 相关阅读:
    数学+高精度 ZOJ 2313 Chinese Girls' Amusement
    最短路(Bellman_Ford) POJ 1860 Currency Exchange
    贪心 Gym 100502E Opening Ceremony
    概率 Gym 100502D Dice Game
    判断 Gym 100502K Train Passengers
    BFS POJ 3278 Catch That Cow
    DFS POJ 2362 Square
    DFS ZOJ 1002/HDOJ 1045 Fire Net
    组合数学(全排列)+DFS CSU 1563 Lexicography
    stack UVA 442 Matrix Chain Multiplication
  • 原文地址:https://www.cnblogs.com/-huyue-/p/12005947.html
Copyright © 2011-2022 走看看