课后作业

1,源代码

import java.io.*;

import java.nio.file.*;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.Scanner;

public class File1 extends SimpleFileVisitor<Path> {

@Override

public FileVisitResult postVisitDirectory(Path dir, IOException exc)

throws IOException {

System.out.println();

System.out.println("Just visited "+dir);

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)

throws IOException {

System.out.println("Aoout to visit "+dir);

System.out.println();

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)

throws IOException {

if(attrs.isRegularFile()){

System.out.println("");

}

System.out.println(file);

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult visitFileFailed(Path file, IOException exc)

throws IOException {

System.err.println(exc.getMessage());

return FileVisitResult.CONTINUE;

}

public static void main(String[] args) throws IOException{

Scanner input=new Scanner(System.in);

String setSecret,secret;

File file=new File("E:\\记事本\\记事本1.txt");

System.out.println("文件总容量:"+file.length()+"字节");

System.out.println("请为该文件设置密码:");

setSecret=input.nextLine();

System.out.println("请输入密码查看文件内容");

do{

secret=input.nextLine();

if(secret.equals(setSecret)){

System.out.println("该E:\\记事本文件夹下的小文件为:");

Path fileDirPath=Paths.get("E:\\记事本");

File1 visit=new File1();

try {

Files.walkFileTree(fileDirPath, visit);

} catch (IOException e) {

e.printStackTrace();

}

InputStreamReader r1=new FileReader("E:\\记事本\\记事本1.txt");

FileReader r2=new FileReader("E:\\记事本\\记事本.txt");

BufferedReader br1=new BufferedReader(r1);

BufferedReader br2=new BufferedReader(r2);

String str;

System.out.println("记事本1里的内容");

while((str=br1.readLine())!=null){

System.out.println(str);

}

System.out.println("记事本里的内容");

while((str=br2.readLine())!=null){

System.out.println(str);

}

}

else

System.out.println("密码错误!请重新输入:");

}while(secret!=setSecret);

}

}

2,结果截图

动手动脑

1,

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

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.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
public class Search implements FileVisitor {
 private final PathMatcher matcher;
 private final long accepted_size;
 public Search(String glob,long accepted_size) {
      matcher= 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 = 1048576;//1M=1024k=1048576字节
    Path fileTree = Paths.get("C:/");
    Search walk=new Search(glob, size);
    EnumSet opts=EnumSet.of(FileVisitOption.FOLLOW_LINKS);
    System.out.println("C盘中大小等于1M的文件有");
    Files.walkFileTree(fileTree, opts, Integer.MAX_VALUE, walk);
   }
}

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

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 FileGlobNIO {

public static void main(String args[]) throws IOException {
        String glob = "glob:**/*.{java,txt}";
        String path = "C:/";
        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文件。

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 FileGlobNIO {

public static void main(String args[]) throws IOException {
        String glob = "glob:**/*.txt";
        String path = "C:/";
        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=="asdfghjkl")//若读取的内容等于“asdfghjkl"则输出文件名
                   {
                         System.out.println(path);
                         break;
                   }
                  }
                }
                  return FileVisitResult.CONTINUE;
            }

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

}

2,实现监控文件夹功能

Path类实现了Watchable接口,因此我们能监控它的变化。

示例FileWatchDemo.java展示了如何监控一个文件夹中文件的新增、删除和改名。

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

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 之间的关联类。

java流与文件的操作 文件加密的更多相关文章

  1. 007PHP文件处理—— 判断文件与操作文件fopen fread fclose is_executable is_readable is_writeable

    <?php /** * 判断文件与操作文件fopen fread fclose * is_executable is_readable is_writeable */ //判断一个文件是不是一个 ...

  2. AIR文件操作:使用文件对象操作文件和目录 .

    来源:http://blog.csdn.net/zdingxin/article/details/6635376 在AIR中可以方便的对本地文件操作,不过上次做了个项目,发现还是有不少不方便的地方,比 ...

  3. AIR使用文件对象操作文件和目录

    文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在AIR中可用. 文件对象能做啥? 获取特定目录,包括用户目录.用户文档目录.该应用程序启动的目录和程序目录 ...

  4. AIR文件操作(二):使用文件对象操作文件和目录

    转载于:http://www.flashj.cn/wp/air-file-operation2.html 文件对象是啥?文件对象(File对象)是在文件系统中指向文件或目录的指针.由于安全原因,只在A ...

  5. PHP文件处理--操作文件

    除了能够对文件内容进行读写,对文件本身相同也能够进行操作,如拷贝文件.又一次命名.查看改动日期等. PHP内置了大量的文件操作函数,经常使用的文件函数例如以下表: 函数原型 函数说明 举例 bool ...

  6. Java 流的概述及操作(转)

    一.什么是流? 流就是字节序列的抽象概念,能被连续读取数据的数据源和能被连续写入数据的接收端就是流,流机制是Java及C++中的一个重要机制,通过流我们可以自由地控制文件.内存.IO设备等数据的流向. ...

  7. 【Python】 文件和操作文件方法

    文件 ■ 基本的文件用法 f = open("path","mode") mode有a,w,r,b,+等.默认为r.模式与打开文件时的动作有关系,比如用w打开的 ...

  8. 2021-2-19:请问你知道 Java 如何高性能操作文件么?

    一般高性能的涉及到存储框架,例如 RocketMQ,Kafka 这种消息队列,存储日志的时候,都是通过 Java File MMAP 实现的,那么什么是 Java File MMAP 呢? 什么是 J ...

  9. 用C#操作文件/文件夹(删除,复制,移动)

    操作某一个文件/文件夹,需要一个文件的完整路径 一.使用File的静态方法进行文件操作 //使用file的静态方法进行复制 File.Copy(path, destpath); //使用File的静态 ...

随机推荐

  1. servlet中地址详细分析

    path路径的写法 假设; 项目名为day01 webroot下存放静态文件demo.html 转发 request.getRequestDispatcherType("path" ...

  2. IOS - ImagePicker 连拍

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)sel ...

  3. [USACO18FEB] Snow Boots G (离线+并查集)

    题目大意:略 网上各种神仙做法,本蒟蒻只想了一个离线+并查集的做法 对所有靴子按最大能踩的深度从大到小排序,再把所有地砖按照积雪深度从大到小排序 一个小贪心思想,我们肯定是在 连续不能踩的地砖之前 的 ...

  4. POJ 2774 Long Long Message (后缀数组+二分)

    题目大意:求两个字符串的最长公共子串长度 把两个串接在一起,中间放一个#,然后求出height 接下来还是老套路,二分出一个答案ans,然后去验证,如果有连续几个位置的h[i]>=ans,且存在 ...

  5. 小学生都能学会的python(小数据池)

    小学生都能学会的python(小数据池) 1. 小数据池. 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建过多的对象 缓存:int, str, bool. int: 缓存范围 -5~256 ...

  6. 使用剩余参数代替 arguments (prefer-rest-params)

    使用剩余参数代替 arguments (prefer-rest-params) 剩余参数来自于ES2016.可以在可变函数中使用这个特性来替代arguments变量. arguments没有Array ...

  7. php 中引入邮箱服务 , 利用第三方的smtp邮件服务

    项目中用短信通知有时间限制,对一些频率比较大的信息力不从心. 使用邮箱发送信息是个不错的选择\(^o^)/! 首先要注册一个邮箱,在邮箱设置里开通smtp功能. 简单介绍下smtp,大概就是第三方客户 ...

  8. [Gatsby] Install Gatsby and Scaffold a Blog

    In this lesson, you’ll install Gatsby and the plugins that give the default starter the ability to t ...

  9. 关闭 sftp

    vi /etc/ssh/sshd_config 注释掉这行Subsystem  sftp    /usr/libexec/openssh/sftp-server /etc/rc.d/init.d/ss ...

  10. Baby_Step,Gaint_Step(分析具体解释+模板)

    下面是总结自他人博客资料.以及本人自己的学习经验. [Baby_Step,Gaint_Step定义] 高次同余方程. BL == N (mod P) 求解最小的L.因为数据范围非常大,暴力不行 这里用 ...