zoukankan      html  css  js  c++  java
  • Day19 io01作业

    1.有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
    String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
    根据单词性质动词verb全部存入verb.txt文件中
    根据单词性质名词noun全部存入noun.txt文件中

    2. 递归查找指定目录中(包括子目录中),所有的.java文件,
    并且,把所有这些找到的java文件,复制到一个指定的目录下
    目录结构同昨天,递归删除那道题的firstLevel

    参考答案:

     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 
     6 
     7 public class Demo{
     8 
     9     public static void main(String[] args) {
    10         String[] words = { "verb:eat", "verb:drink", "verb:sleep", "verb:play", "noun:rice", "noun:meat", "noun:hand",
    11                 "noun:hair" };
    12         String verb = "verb";
    13         String noun = "noun";
    14         // 将所有的动词和名词分别拼接到verbs和nouns两个字符串中
    15         // 让后直接将包含所有 动词的字符串verbs 和 包含所有 名词的字符串nouns,分别写入verb.txt和noun.txt中
    16         String verbs = "";
    17         String nouns = "";
    18         for (String word : words) {
    19             if (word.contains(verb)) {
    20                 verbs += word;
    21                 verbs += "
    ";
    22             }
    23             if (word.contains(noun)) {
    24                 nouns += word;
    25                 nouns += "
    ";
    26             }
    27         }
    28         File verbFile = new File("D:\WorkSpace\verb.txt");
    29         File nounFile = new File("D:\WorkSpace\noun.txt");
    30         FileOutputStream verbFileOutputStream = null;
    31         FileOutputStream nounFileOutputStream = null;
    32         try {
    33             // 初始化输出流
    34             verbFileOutputStream = new FileOutputStream(verbFile);
    35             nounFileOutputStream = new FileOutputStream(nounFile);
    36             // 获得字节数组
    37             byte[] verbBytes = verbs.getBytes();
    38             byte[] nounBytes = nouns.getBytes();
    39             // 输出
    40             verbFileOutputStream.write(verbBytes);
    41             nounFileOutputStream.write(nounBytes);
    42         } catch (FileNotFoundException e) {
    43             e.printStackTrace();
    44         } catch (IOException e) {
    45             e.printStackTrace();
    46         } finally {
    47             // 资源释放
    48             try {
    49                 if (verbFileOutputStream != null) {
    50                     verbFileOutputStream.close();
    51                 }
    52                 if (nounFileOutputStream != null) {
    53                     nounFileOutputStream.close();
    54                 }
    55             } catch (IOException e) {
    56                 e.printStackTrace();
    57             }
    58 
    59         }
    60     }
    61 
    62 }

    2.

      1 import java.io.BufferedInputStream;
      2 import java.io.BufferedOutputStream;
      3 import java.io.Closeable;
      4 import java.io.File;
      5 import java.io.FileFilter;
      6 import java.io.FileInputStream;
      7 import java.io.FileNotFoundException;
      8 import java.io.FileOutputStream;
      9 import java.io.IOException;
     10 import java.io.InputStream;
     11 import java.io.OutputStream;
     12 
     13 public class Demo2 {
     14 
     15   public static void main(String[] args) {
     16     FileHandler fileHandler = null;
     17     try {
     18       //要查找的目录
     19       File dir = new File("d:\firstLevel");
     20       //利用fileHandler对象,完成递归查找,并将查找结果,存到d:
    esult目录中
     21       fileHandler = new FileHandler("d:\result");
     22 
     23       //由FileFilter接口的匿名内部类对象,指明目标文件的筛选条件,开始递归查找,
     24       // 并对找到的目标文件做相应处理
     25       fileHandler.findFile(dir, new FileFilter() {
     26         @Override
     27         public boolean accept(File pathname) {
     28           return pathname != null
     29               && pathname.isFile()
     30               && pathname.getName().endsWith(".java");
     31         }
     32       });
     33     } catch (FileNotFoundException e) {
     34       e.printStackTrace();
     35     } catch (IOException e) {
     36       e.printStackTrace();
     37     }
     38   }
     39 }
     40 
     41 /*
     42       利用该类,完成文件的递归查找功能
     43  */
     44 class FileHandler {
     45   //保存指定目录中,所有java文件的目录路径
     46   File dir;
     47 
     48   public FileHandler(String recordPath) throws FileNotFoundException {
     49     //利用目标文件的路径字符串,初始化,表示用来存储文件名的文件的File对象
     50     dir = new File(recordPath);
     51     //确保目标目录存在
     52     dir.mkdirs();
     53   }
     54 
     55   /*
     56         利用该方法,使用由FileFilter对象,所指明的筛选条件,筛选出符合条件的文件
     57         并将这些文件的绝对路径,保存到指定文件中
     58    */
     59   public void findFile(File file, FileFilter filter) throws IOException {
     60     File[] files = file.listFiles();
     61 
     62     if (files == null) {
     63       //说明当前File dir其实表示的是一个文件
     64 
     65       if (filter.accept(file)) {
     66         //满足筛选条件,就将满足条件的文件复制到目标目录下
     67 
     68         //创建File对象,表示复制的目标文件
     69         File destFile = new File(dir, file.getName());
     70         copyFile(file, destFile);
     71 
     72       }
     73       return;
     74     }
     75     for (int i = 0; i < files.length; i++) {
     76       findFile(files[i], filter);
     77     }
     78   }
     79 
     80   //利用缓冲流完成文件的复制
     81   public void copyFile(File srcFile, File destFile) {
     82 
     83 
     84     // 创建缓冲输入字节流对象
     85     InputStream in = null;
     86 
     87     // 创建缓冲字节输出流对象
     88     OutputStream out = null;
     89     try {
     90       in = new BufferedInputStream(
     91           new FileInputStream(srcFile));
     92 
     93       out = new BufferedOutputStream(
     94           new FileOutputStream(destFile));
     95 
     96       int len;
     97       byte[] buffer = new byte[1024];
     98 
     99       //复制文件
    100       while((len = in.read(buffer)) != -1) {
    101         out.write(buffer, 0, len);
    102       }
    103     } catch (FileNotFoundException e) {
    104       e.printStackTrace();
    105     } catch (IOException e) {
    106       e.printStackTrace();
    107     } finally {
    108       //关闭输入流
    109       closeQuietly(in);
    110       //关闭输出流
    111       closeQuietly(out);
    112     }
    113 
    114 
    115   }
    116 
    117   private void closeQuietly(Closeable closeable) {
    118 
    119     //关闭输出流
    120     try {
    121       if (closeable != null) {
    122         closeable.close();
    123       }
    124 
    125     } catch (IOException e) {
    126       e.printStackTrace();
    127     }
    128 
    129   }
    130 
    131 }
  • 相关阅读:
    bfs入门 (HDU
    Codeforces Round #570 (Div. 3)B
    nyoj 277-车牌号 (map, pair, iterator)
    nyoj 276-比较字母大小 (顺序比较, 逆序输出)
    nyoj 275-队花的烦恼一 (stack, push, pop)
    nyoj 274-正三角形的外接圆面积 (R = PI * a * a / 3)
    nyoj 273-字母小游戏 (getline(cin, string))
    nyoj 268-荷兰国旗问题 (count)
    nyoj 266-字符串逆序输出 (isdigit(), geline(cin, my_string))
    nyoj 264-国王的魔镜 (string[-1:-int(str_len/2+1):-1])
  • 原文地址:https://www.cnblogs.com/dust2017/p/12791504.html
Copyright © 2011-2022 走看看