Java文件与类动手动脑实例详解

yipeiwu_com6年前Python基础

动手动脑1:

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

package classJava;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;


public class titletwo implements FileVisitor<Object> {
 private long accepted_size;
 public void titletwo(String glob,long accepted_size) {
   FileSystems.getDefault().getPathMatcher("glob:" +glob);
   this.accepted_size=accepted_size; 
  }
  void search(Path file) throws IOException {
  long size = (Long) Files.getAttribute(file, "basic:size");
  if(size ==accepted_size) {  
    System.out.println(file);
  }
  
  }
  
  @Override
  public FileVisitResult postVisitDirectory(Object dir, IOException exc)throws IOException {  
    return FileVisitResult.CONTINUE;
  }
  
  @Override
  public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)throws IOException {  
    return FileVisitResult.CONTINUE;
  }
  
  @Override
  public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)throws IOException {
  search((Path) file);   
  return FileVisitResult.CONTINUE;
  }
  
  @Override
  public FileVisitResult visitFileFailed(Object file, IOException exc)throws IOException { 
    return FileVisitResult.CONTINUE;
  }
   
  public static void main(String[] args) throws IOException{ 
    String glob= "*.jpg";   
    long size = 28672;  
    Path fileTree = Paths.get("D:/"); 
    titletwo walk=new titletwo();  
    EnumSet<FileVisitOption> opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);  
    System.out.println("D盘中大小等于28672字节的文件有");  
    Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
  }
}

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

package classJava;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class titletwo {

  public static void main(String args[]) throws IOException {
    String glob = "glob:**/*.{java,txt}";
    String path = "D:/";
    match(glob, path);
  }

  public static void match(String glob, String location) throws IOException {

    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);

    Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

      @Override
      public FileVisitResult visitFile(Path path,
          BasicFileAttributes attrs) throws IOException {
        if (pathMatcher.matches(path)) {
          System.out.println(path);
        }
        return FileVisitResult.CONTINUE;
      }

      @Override
      public FileVisitResult visitFileFailed(Path file, IOException exc)
          throws IOException {
        return FileVisitResult.CONTINUE;
      }
    });
  }

}

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

package classJava;

import java.io.IOException;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class titletwo {

  public static void main(String args[]) throws IOException {
    String glob = "glob:**/*.txt";
    String path = "D:\\wenjian";
    match(glob, path);
  }

  public static void match(String glob, String location) throws IOException {

    final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob);

    Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

      @Override
      public FileVisitResult visitFile(Path path,
          BasicFileAttributes attrs) throws IOException {
        if (pathMatcher.matches(path)) {
         BufferedReader reader =Files.newBufferedReader(path);//读取文件内的内容 
         String line=null;
         while((line = reader.readLine())!=null) {
          if(line.equals("account"))//若读取的内容等于“account"则输出文件名
          {
             System.out.println(path);
             break;
          }
          
         }
        }
         return FileVisitResult.CONTINUE;
      }

      @Override
      public FileVisitResult visitFileFailed(Path file, IOException exc)
          throws IOException {
        return FileVisitResult.CONTINUE;
      }
    });
  }

}

动手动脑2:

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

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

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

以上就是本次介绍的关于Java文件与类动手动脑实例的全部知识点,感谢大家的学习和对【听图阁-专注于Python设计】的支持。

相关文章

Django ImageFiled上传照片并显示的方法

1:首先理解settings.py中 MEDIA_ROOT: MEDIA_URL:这两者之间的关系。 MEDIA_ROOT:就是保存上传图片的根目录,比如说MEIDA_ROOT ="C:...

python3安装pip3(install pip3 for python 3.x)

python3安装pip3(install pip3 for python 3.x)

前言:   我目前使用的服务器为centos6.x 系统自带的python的版本为2.6.x,但是目前无论是学习还是使用python,python3都是首选,那么问题来了。---如何安装...

Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项

Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项

1、致欢迎词 我将详细讲述在学Python初期的各种手忙脚乱的问题的解决,通过这些步骤的操作,让你的注意力集中在Python的语法上以及后面利用Python所解决的项目问题上。而我自己作...

python下函数参数的传递(参数带星号的说明)

函数参数的使用又有俩个方面值得注意:1.函数参数是如何定义的 2.在调用函数的过程中参数是如何被解析先看第一个问题,在python中函数参数的定义主要有四种方式: 1.F(arg1,ar...

Python加密方法小结【md5,base64,sha1】

本文实例总结了python加密方法。分享给大家供大家参考,具体如下: MD5加密: def md5(str): import hashlib m = hashlib.md5(...