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

最新文章

  1. 初试WIX加SQL LocalDB
  2. win10连vpn
  3. HBase中批量修改
  4. sqoop与mysql之间中文乱码
  5. [solr] - IKAnalyzer 分词加入
  6. readDouble
  7. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup
  8. 关于python中使用mongodb模块,save和insert的小问题
  9. 火狐和google游览器的 hack独有识别 css
  10. SQlSERVER生成唯一编号
  11. virtual 关键字
  12. js优化与注意点
  13. Android开发获取多个存储空间的路径(内置SD卡以及外置SD卡)
  14. MARKY一下。
  15. windows系统实现mysql数据库数据库主从复制
  16. HDU-6033 Add More Zero
  17. lombok-@Accessors注解
  18. sqlserver中为节约存储空间的收缩数据库机制
  19. Linux命令-终止进程命令:pkill
  20. jquery easyUi columns日期格式化

热门文章

  1. JSOI2009 密码 和 JSOI2007 文本生成器 和 ZOJ3545 Rescue the Rabbit
  2. 用CSS 实现 非浮动元素的 水平居中/垂直居中/水平垂直居中
  3. RedisTemplate和StringRedisTemplate的区别
  4. AfxBeginThread深入解析
  5. ajax当有返回值时
  6. h5css3弹性盒子
  7. 005_simulink建立条件子系统
  8. GreenPlum 数据库启动关闭及数据库状态检查
  9. scrapy 分布式爬虫- RedisSpider
  10. Python的模块,模块的使用、安装,别名,模块作用域