动手动脑06+课后作业

1.使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如500M)的文件。

 1 package dsydn1;
 2 import java.io.IOException;
 3 import java.nio.file.FileSystems;
 4 import java.nio.file.FileVisitOption;
 5 import java.nio.file.FileVisitResult;
 6 import java.nio.file.FileVisitor;
 7 import java.nio.file.Files;
 8 import java.nio.file.Path;
 9 import java.nio.file.PathMatcher;
10 import java.nio.file.Paths;
11 import java.nio.file.attribute.BasicFileAttributes;
12 import java.util.EnumSet;
13 public class Search implements FileVisitor {
14  private final PathMatcher matcher;
15  private final long accepted_size;
16  public Search(String glob,long accepted_size) {
17       matcher= FileSystems.getDefault().getPathMatcher("glob:" +glob);
18       this.accepted_size=accepted_size; 
19     }
20    void search(Path file) throws IOException {
21     long size = (Long) Files.getAttribute(file, "basic:size");
22     if(size >=accepted_size) {
23      System.out.println(file);
24     }
25    }
26    @Override
27    public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException {
28     return FileVisitResult.CONTINUE;
29    }
30    @Override
31    public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException {
32     return FileVisitResult.CONTINUE;
33    }
34    @Override
35    public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException {
36   search((Path) file);
37      return  FileVisitResult.CONTINUE;
38   }
39    @Override
40    public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException {
41   return FileVisitResult.CONTINUE;
42    }
43    public static void main(String[] args) throws IOException{
44     System.out.println("D盘中大小大于500M的文件有");
45     String glob= "*.txt";
46     long size = 1048576*500;//1M=1024k=1048576字节
47     Path fileTree = Paths.get("D:/");
48     Search walk=new Search(glob, size);
49     EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);
50     
51     Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
52    }
53 }

2.使用Files. walkFileTree()找出指定文件夹下所有扩展名为.txt和.exe的文件。

1 package dsydn2;
 2 import java.io.IOException;
 3 import java.nio.file.FileSystems;
 4 import java.nio.file.FileVisitResult;
 5 import java.nio.file.Files;
 6 import java.nio.file.Path;
 7 import java.nio.file.PathMatcher;
 8 import java.nio.file.Paths;
 9 import java.nio.file.SimpleFileVisitor;
10 import java.nio.file.attribute.BasicFileAttributes;
11 public class FileGlobNIO {
12     public static void main(String args[]) throws IOException {
13         String glob = "glob:**/*.{exe,txt}";
14         String path = "D:/";
15         match(glob, path);
16     }
17     public static void match(String glob, String location) throws IOException {
18         final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);
19         Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
20             @Override
21             public FileVisitResult visitFile(Path path,
22                     BasicFileAttributes attrs) throws IOException {
23                 if (pathMatcher.matches(path)) {
24                     System.out.println(path);
25                 }
26                 return FileVisitResult.CONTINUE;
27             }
28             @Override
29             public FileVisitResult visitFileFailed(Path file, IOException exc)
30                     throws IOException {
31                 return FileVisitResult.CONTINUE;
32             }
33         });
34     }
35 }

3. 使用Files. walkFileTree()找出指定文件夹下所有包容指定字符串的txt文件。

 1 package dsydn3;
 2 import java.io.IOException;
 3  import java.io.*;
 4  import java.nio.file.FileSystems;
 5  import java.nio.file.FileVisitResult;
 6  import java.nio.file.Files;
 7  import java.nio.file.Path;
 8  import java.nio.file.PathMatcher;
 9  import java.nio.file.Paths;
10  import java.nio.file.SimpleFileVisitor;
11  import java.nio.file.attribute.BasicFileAttributes;
12  
13  public class FileGlobNIO {
14  
15      public static void main(String args[]) throws IOException {
16          String glob = "glob:**/*.txt";
17          String path = "D:/";
18          match(glob, path);
19      }
20  
21      public static void match(String glob, String location) throws IOException {
22  
23          final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);
24  
25          Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {
26  
27              @Override
28              public FileVisitResult visitFile(Path path,
29                      BasicFileAttributes attrs) throws IOException {
30                  if (pathMatcher.matches(path)) {
31                   BufferedReader reader =Files.newBufferedReader(path);
32                    String line=null;
33                    while((line = reader.readLine())!=null) {
34                     if(line.equals("public"))
35                     {
36                           System.out.println(path);
37                           break;
38                     }
39                     
40                    }
41                  }
42                    return FileVisitResult.CONTINUE;
43              }
44  
45              @Override
46              public FileVisitResult visitFileFailed(Path file, IOException exc)
47                      throws IOException {
48                  return FileVisitResult.CONTINUE;
49              }
50          });
51      }
52  }

4.请通过查询JDK文件和使用搜索引擎等方式,看懂此示例代码,并弄明白Watchable、WatchService等类型之间的关系,使用UML类图表示出这些类之间的关系? 

WatchService 
看作是文件监控器,通过操作系统原生文件系统来运行。 
针对单点多appkey的情况,可以注册开启多个监控器。 
每个监控器可看作是后台线程,通过监控文件发出的信号来实现监控。

WatchService 用来观察被注册了的对象所有的变化和事件

Watchable 被观察者,与WatchService结合使用, java.nio.file.Path 已经实现 

WatchService 实例化: 

WatchService watchService = FileSystems.getDefault().newWatchService(); 

利用 Path 实例化监控对象 Watchable 

Path dir = Paths.get(path); 

将 Path 注册到 WatchService 中//这里监控文件的 创建、修改、删除  但是这里返回的key里面的监控信息为空 

WatchKey key = dir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE);   

监控池是静态的,只有当你主动去获取新的监控池时才会有更新的内容加入监控池。这就造成了系统接收到监控信息事件可能稍长的问题。 

1,java.nio.file.WatchService文件系统监视服务的接口类,它的具体实现由监视服务提供者负责加载。 

2,ava.nio.file.Watchable 实现了 java.nio.file.Watchable 的对象才能注册监视服务 WatchService。java.nio.file.Path实现了 watchable 接口,后文使用 Path 对象注册监视服务。 

 3,java.nio.file.WatchKey 该类代表着 Watchable 对象和监视服务 WatchService 的注册关系。WatchKey 在 Watchable 对象向 WatchService 注册的时候被创建。它是 Watchable 和 WatchService 之间的关联类。

课后作业:

一:编写一个程序,指定一个文件夹,能自动计算出其总容量

 1 package class6;
 2 
 3 import java.io.File;
 4 import java.util.ArrayList;
 5  
 6 public class Size {
 7    static long size=0;
 8  private static ArrayList<String> filelist=new ArrayList<String>();
 9  public static void main(String[] args) {
10   Size s=new Size();
11   String filePath="F:\\天梯赛";
12   s.getFiles(filePath);
13   
14  }
15  //通过递归得到某一路径下所有的目录及文件
16 void getFiles(String filePath) {
17   
18  File root=new File(filePath);
19   File[] files=root.listFiles();
20   for(File file:files) {
21   if(file.isDirectory()) {
22     getFiles(file.getAbsolutePath());
23    filelist.add(file.getAbsolutePath());
24    }else {
25     size+=file.getAbsolutePath().length();
26    }
27 System.out.println("大小是"+size);
28   }  
29 }

2.编写一个文件加解密程序,通过命令行完成加解密工作

 1 package text5_3;
 2 import java.io.File;
 3 import java.io.InputStream;
 4 import java.io.OutputStream;
 5 import java.io.FileInputStream;
 6 import java.io.FileOutputStream;
 7 
 8 public class FileCode {
 9  private static final int numOfEncAndDec=0x99;//加密解密密钥
10  private static int dataOfFile=0;//文件字节内容
11 
12  public static void main(String[] args) {
13   File srcFile=new File("D:\\poem.txt");//初始化文件
14   File encFile=new File("D:\\poem.txt"); //加密文件
15   File decFile=new File("D:\\poem.txt");  //解密文件
16  
17   try {
18    EncFile(srcFile,encFile);  //加密操作
19    DecFile(encFile,decFile);
20   }catch(Exception e) {
21    e.printStackTrace();
22   }
23  }
24  private static void EncFile(File srcFile,File encFile)throws Exception{
25   if(!srcFile.exists()) {
26    System.out.println("source file not exixt");
27    }
28   if(!encFile.exists()) {
29    System.out.println("encrypt file created");
30    encFile.createNewFile();//若无加密文件,新建一个加密文件
31   }
32   InputStream fis=new FileInputStream(srcFile);
33   OutputStream fos=new FileOutputStream(encFile);
34   
35   while((dataOfFile=fis.read())>-1) {//当读到文件内容时
36    fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
37   }
38   fis.close();
39   fos.flush();
40   fos.close();
41  }
42  private static void DecFile(File encFile,File decFile)throws Exception{
43   if(!encFile.exists()) {
44    System.out.println("encrypt file not exixt");
45   }
46   if(!decFile.exists()) {
47    System.out.println("decrypt file created");
48    decFile.createNewFile();
49   }
50   InputStream fis=new FileInputStream(encFile);
51   OutputStream fos=new FileOutputStream(decFile);
52   
53   while((dataOfFile=fis.read())>-1) {
54    fos.write(dataOfFile^numOfEncAndDec);
55   }
56   fis.close();
57   fos.flush();
58   fos.close();
59  }
60 
61 }

3.编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件

文件分割:

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 
 8 public class CutFile {
 9     public static void main(String[] args) {
10         //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小)
11         cutFile("D:\\file\\file.txt", "D:\\file2",1024 * 1024 * 20);
12     }
13 
14     private static void cutFile(String src, String endsrc, int num) {
15         FileInputStream fis = null;
16         File file = null;
17         try {
18             fis = new FileInputStream(src);
19             file = new File(src);
20             //创建规定大小的byte数组
21             byte[] b = new byte[num];
22             int len = 0;
23             //name为以后的小文件命名做准备
24             int name = 1;
25             //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中
26             while ((len = fis.read(b)) != -1) {
27                 //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备
28                 String name2 = file.getName();
29                 int lastIndexOf = name2.lastIndexOf(".");
30                 String substring = name2.substring(0, lastIndexOf);
31                 String substring2 = name2.substring(lastIndexOf, name2.length());
32                 FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);
33                 //将byte数组写入对应的小文件中
34                 fos.write(b, 0, len);
35                 //结束资源
36                 fos.close();
37                 name++;
38             }
39         } catch (FileNotFoundException e) {
40             e.printStackTrace();
41         } catch (IOException e) {
42             e.printStackTrace();
43         } finally {
44             try {
45                 if (fis != null) {
46                     //结束资源
47                     fis.close();
48                 }
49             } catch (IOException e) {
50                 e.printStackTrace();
51             }
52         }
53     }
54 }

文件集合:

 1 package class6;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 
 9 public class GotherFile {
10     public static void main(String[] args){
11         //调用togetherFile()函数合并小文件到大文件   参数列表分别为 (小文件所在的父文件夹路径,所合成的大文件的路径)
12         togetherFile("D:\\file2","D:\\file3\\file.txt");
13     }
14     private static void togetherFile(String src, String endsrc){
15         FileOutputStream fos = null;
16         File file1 = null;
17         File file2 = null;
18         try {
19             file1 = new File(endsrc);
20             file2 = new File(src);
21             //获得大文件的存储路径的FileOutputStream对象
22             fos = new FileOutputStream(endsrc);
23             //循环遍历对应文件夹中的所有小文件
24             for(File file : file2.listFiles()){
25 
26                 FileInputStream fis = new FileInputStream(file.getAbsolutePath());
27 
28                 byte[] b = new byte[1024*1024];
29                 int len = 0;
30                 //将小文件读入byte数组,之后再将byte数组写入大文件中
31                 while((len = fis.read(b)) != -1){
32                     fos.write(b, 0, len);
33                 }
34                 //结束资源
35                 fis.close();
36             }
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         }finally{
42             try {
43                 if(fos != null){
44                     //结束资源
45                     fos.close();
46                 }
47             } catch (IOException e) {
48                 e.printStackTrace();
49             }
50         }
51     }
52 }

猜你喜欢

转载自www.cnblogs.com/Excusezuo/p/9981289.html