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用法的更多相关文章

  1. Java NIO Files

    Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ...

  2. Java NIO学习系列七:Path、Files、AsynchronousFileChannel

    相对于标准Java IO中通过File来指向文件和目录,Java NIO中提供了更丰富的类来支持对文件和目录的操作,不仅仅支持更多操作,还支持诸如异步读写等特性,本文我们就来学习一些Java NIO提 ...

  3. Java NIO 完全学习笔记(转)

    本篇博客依照 Java NIO Tutorial翻译,算是学习 Java NIO 的一个读书笔记.建议大家可以去阅读原文,相信你肯定会受益良多. 1. Java NIO Tutorial Java N ...

  4. 海纳百川而来的一篇相当全面的Java NIO教程

    目录 零.NIO包 一.Java NIO Channel通道 Channel的实现(Channel Implementations) Channel的基础示例(Basic Channel Exampl ...

  5. Java NIO Path

    Java NIO Path Creating a Path Instance Creating an Absolute Path Creating a Relative Path Path.norma ...

  6. Java NIO 学习总结 学习手册

    原文 并发编程网(翻译):http://ifeve.com/java-nio-all/  源自 http://tutorials.jenkov.com/java-nio/index.html Java ...

  7. Java NIO Path接口和Files类配合操作文件

    Java NIO Path接口和Files类配合操作文件 @author ixenos Path接口 1.Path表示的是一个目录名序列,其后还可以跟着一个文件名,路径中第一个部件是根部件时就是绝对路 ...

  8. 【Java基础 】Java7 NIO Files,Path 操作文件

    从Java1.0到1.3,我们在开发需要I/O支持的应用时,要面临以下问题: 没有数据缓冲区或通道的概念,开发人员要编程处理很多底层细节 I/O操作会被阻塞,扩展能力有限 所支持的字符集编码有限,需要 ...

  9. JAVA NIO学习四:Path&Paths&Files 学习

    今天我们将学习NIO 的最后一章,前面大部分涉及IO 和 NIO 的知识都已经讲过了,那么本章将要讲解的是关于Path 以及Paths 和 Files 相关的知识点,以对前面知识点的补充,好了言归正传 ...

随机推荐

  1. js 异步执行顺序

    参考文章: js 异步执行顺序   1.js的执行顺序,先同步后异步 2.异步中任务队列的执行顺序: 先微任务microtask队列,再宏任务macrotask队列 3.调用Promise 中的res ...

  2. Mac+appium+iOS 环境搭建

    Mac+appium+iOS 环境搭建,需要用到的信息如下,参考搭建环境. 1.安装brew,安装介绍:https://jingyan.baidu.com/article/fec7a1e5ec3034 ...

  3. 去除IDEA中xml黄色背景

    idea版本:IntelliJ IDEA 2019.2.1 在编写mybatis的xml中会出现大面积黄色背景提示,看起来比较不舒服. 去掉黄色背景颜色 1.打开File->Settings-& ...

  4. MySQL高级 之 explain执行计划详解(转)

    使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈. explain执行计划包含的信息 其中最重要的字段为:i ...

  5. GreenPlum 数据库创建用户、文件空间、表空间、数据库

    前几篇文章介绍了GreenPlum数据库的安装.启动.关闭.状态检查.登录等操作,数据库已经创建好了,接下来介绍如何使用数据库.按照习惯,需要先创建测试用户.表空间.数据库.先创建测试用户dbdrea ...

  6. P1928 外星密码

    题目描述 有了防护伞,并不能完全避免 2012 的灾难.地球防卫小队决定去求助外星种族的帮 助.经过很长时间的努力,小队终于收到了外星生命的回信.但是外星人发过来的却是一 串密码.只有解开密码,才能知 ...

  7. 永远不会被卡的Dinic

    78,79行是精髓 61,148,149行是当前弧优化 #include <cstring> #include <cstdio> #include <queue> ...

  8. NetworkX系列教程(10)-算法之五:广度优先与深度优先

    小书匠Graph图论 重头戏部分来了,写到这里我感觉得仔细认真点了,可能在NetworkX中,实现某些算法就一句话的事,但是这个算法是做什么的,用在什么地方,原理是怎么样的,不清除,所以,我决定先把图 ...

  9. (1)打鸡儿教你Vue.js

    当今世界不会Vue.js,前端必定路难走 一个JavaScript MVVM库 以数据驱动和组件化的思想构建的 Vue.js是数据驱动 HTML/CSS/JavaScript/ES6/HTTP协议/V ...

  10. 在docker容器中编译hadoop 3.1.0

    在docker容器中编译hadoop 3.1.0 优点:docker安装好之后可以一键部署编译环境,不用担心各种库不兼容等问题,编译失败率低. Hadoop 3.1.0 的源代码目录下有一个 `sta ...