java nio Files.newDirectoryStream用法
try(DirectoryStream<Path> dirStream = Files.newDirectoryStream(Paths.get(directory,"*.ts"))){
            byte[] buff = Files.readAllBytes(Paths.get(m3u8File));
            String playList = new String(buff);
            int cntTSFiles = Iterators.size(dirStream.iterator());
            HashMap<String, Object> memMapping = Maps.newHashMapWithExpectedSize(cntTSFiles + 2);
            //播放清单
            memMapping.put("playlist",playList);
            //把原始文件放进去,方便以后下载
            memMapping.put("binary",Base64.getEncoder().encodeToString(Files.readAllBytes(Paths.get(originalWavFile))));
            Iterator<Path> iterator = dirStream.iterator();
            while (iterator.hasNext()) {
                Path path = iterator.next();
                String binary = Base64.getEncoder().encodeToString(Files.readAllBytes(path));
                String tsFile = path.getFileName().toString();
                memMapping.put(tsFile,binary);
            }
            String mediaId = String.format("media.%s",uuid);
            //切片以后的文件添加到缓存
            cacheService.setCacheMap(mediaId, memMapping);
            //一周以后失效
            cacheService.expire(mediaId,7, TimeUnit.DAYS);
        }catch (IOException ex)
        {
            log.error("读取MPEG-2 TS文件失败:{}",ex);
        }
网络代码:
private List<IOException> tryRemoveDirectoryContents(@Nonnull Path path) {
  Path normalized = path.normalize();
  List<IOException> accumulatedErrors = new ArrayList<>();
  if (!Files.isDirectory(normalized)) return accumulatedErrors;
  try (DirectoryStream<Path> children = Files.newDirectoryStream(normalized)) {
    for (Path child : children) {
      accumulatedErrors.addAll(tryRemoveRecursive(child));
    }
  } catch (IOException e) {
    accumulatedErrors.add(e);
  }
  return accumulatedErrors;
}
 public static void main(String... args) throws IOException {
        String pathString = System.getProperty("java.io.tmpdir");
        Path path = Paths.get(pathString);
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
            Iterator<Path> iterator = ds.iterator();
            int c = 0;
            while (iterator.hasNext() && c < 5) {
                Path p = iterator.next();
                System.out.println(p);
                c++;
            }
        }
    }
 public static void main(String... args) throws IOException {
        String pathString = System.getProperty("java.io.tmpdir");
        Path path = Paths.get(pathString);
        System.out.println("Path to stream: " + path);
        //stream all files with name ending .log
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(path,
                p -> p.getFileName().toString().startsWith("aria"))) {
            ds.forEach(System.out::println);
        }
    }
 private static DirectoryStream<Path> list(Path dir) throws IOException {
  return Files.newDirectoryStream(dir, entry -> !DirectoryLock.LOCK_FILE_NAME.equals(entry.getFileName().toString()));
 }
private void forEachTopologyDistDir(ConsumePathAndId consumer) throws IOException {
  Path stormCodeRoot = Paths.get(ConfigUtils.supervisorStormDistRoot(conf));
  if (Files.exists(stormCodeRoot) && Files.isDirectory(stormCodeRoot)) {
    try (DirectoryStream<Path> children = Files.newDirectoryStream(stormCodeRoot)) {
      for (Path child : children) {
        if (Files.isDirectory(child)) {
          String topologyId = child.getFileName().toString();
          consumer.accept(child, topologyId);
        }
      }
    }
  }
}
/**
* 复制目录
*/
public static void copyDir(@NotNull Path from, @NotNull Path to) throws IOException {
Validate.isTrue(isDirExists(from), "%s is not exist or not a dir", from);
Validate.notNull(to);
makesureDirExists(to);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(from)) {
for (Path path : dirStream) {
copy(path, to.resolve(path.getFileName()));
}
}
}
private void copyRecursively( Path source, Path target ) throws IOException
{
try ( DirectoryStream<Path> directoryStream = Files.newDirectoryStream( source ) )
{
for ( Path sourcePath : directoryStream )
{
Path targetPath = target.resolve( sourcePath.getFileName() );
if ( Files.isDirectory( sourcePath ) )
{
Files.createDirectories( targetPath );
copyRecursively( sourcePath, targetPath );
}
else
{
Files.copy( sourcePath, targetPath,
REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES );
}
}
}
}
代码来源:https://www.logicbig.com/how-to/code-snippets/jcode-java-io-files-newdirectorystream.html
https://www.codota.com/code/java/methods/java.nio.file.Files/newDirectoryStream
java nio Files.newDirectoryStream用法的更多相关文章
- Java NIO Files
		Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ... 
- Java NIO学习系列七:Path、Files、AsynchronousFileChannel
		相对于标准Java IO中通过File来指向文件和目录,Java NIO中提供了更丰富的类来支持对文件和目录的操作,不仅仅支持更多操作,还支持诸如异步读写等特性,本文我们就来学习一些Java NIO提 ... 
- Java NIO 完全学习笔记(转)
		本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ... 
- 海纳百川而来的一篇相当全面的Java NIO教程
		目录 零.NIO包 一.Java NIO Channel通道 Channel的实现(Channel Implementations) Channel的基础示例(Basic Channel Exampl ... 
- Java NIO Path
		Java NIO Path Creating a Path Instance Creating an Absolute Path Creating a Relative Path Path.norma ... 
- Java NIO 学习总结 学习手册
		原文 并发编程网(翻译):http://ifeve.com/java-nio-all/ 源自 http://tutorials.jenkov.com/java-nio/index.html Java ... 
- Java NIO Path接口和Files类配合操作文件
		Java NIO Path接口和Files类配合操作文件 @author ixenos Path接口 1.Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路 ... 
- 【Java基础 】Java7 NIO Files,Path 操作文件
		从Java1.0到1.3,我们在开发需要I/O支持的应用时,要面临以下问题: 没有数据缓冲区或通道的概念,开发人员要编程处理很多底层细节 I/O操作会被阻塞,扩展能力有限 所支持的字符集编码有限,需要 ... 
- JAVA NIO学习四:Path&Paths&Files 学习
		今天我们将学习NIO 的最后一章,前面大部分涉及IO 和 NIO 的知识都已经讲过了,那么本章将要讲解的是关于Path 以及Paths 和 Files 相关的知识点,以对前面知识点的补充,好了言归正传 ... 
随机推荐
- 26.C# 文件系统
			1.流的含义 流是一系列具有方向性的字节序列,比如水管中的水流,只不过现在管道中装的不是水,而是字节序列.当流是用于向外部目标比如磁盘输出数据时称为输出流,当流是用于把数据从外部目标读入程序称为输入流 ... 
- NAT和PAT
			地址转换技术 优点: 内网能够主动访问外网 外网不能主动访问内网 内网安全 节省公网ip地址 缺点:慢 PAT 端口地址转换 节省公网IP 替换源端口和源地址 NAT 不节省公网IP 一个公网地址 ... 
- 学到了林海峰,武沛齐讲的Day21-完  模块和包
			调用包,会执行包的__init__.py "IF__name__=='__main__':执行当前文件会执行" time random 开始玩高级的了.. 爽 
- 四十.创建Redis集群 管理集群
			环境准备 准备 6台(51-56) redis服务器 以默认配置运行redis服务即可 一.创建Redis集群 1.启用集群功能( 51-56 都要配置) ]# netstat -antupl ... 
- 二十八. Ceph概述 部署Ceph集群 Ceph块存储
			client :192.168.4.10 node1 :192.168.4.11 ndoe2 :192.168.4.12 node3 :192.168.4.13 1.实验环境 准备四台KVM虚 ... 
- msyql的子查询,或者叫嵌套查询
			INNER和OUTER可以省略 
- 节点(node)操作
			一.节点的属性 节点值页面中的所有内容,包括标签.属性.文本 nodeType,节点类型:如果是标签,则是1:如果是属性.则是2:如果是文本,则是3 nodeName,节点名字:如果是标签,则是大写的 ... 
- [Luogu] 仓鼠找sugar
			https://www.luogu.org/problemnew/show/3398 树剖练习题,两个懒标记,搜索时序为全局懒标记 #include <bits/stdc++.h> usi ... 
- linux系列(二十四):du命令
			1.命令格式 du [选项][文件] 2.命令功能 显示每个文件和目录的磁盘使用空间. 3.命令参数 -a或-all 显示目录中个别文件的大小. -b或-bytes 显示目录或文件大小时,以byte为 ... 
- Linux Soft-RoCE implementation (zz)
			Linux Soft-RoCE implementation 首页分类标签留言关于订阅2017-11-08 | 分类 Network | 标签 RDMA RoCE Linux-RDMA 内核在4 ... 
