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 相关的知识点,以对前面知识点的补充,好了言归正传 ...
随机推荐
- BZOJ4706 B君的多边形 (超级卡特兰数/施罗德数)
题目 权限题QAQ 题解 超级卡特兰数/施罗德数 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 1 ...
- Longest Continuous Increasing Subsequence
Description Give an integer array,find the longest increasing continuous subsequence in this array. ...
- E:nth-of-type(n)
E:nth-of-type(n) 语法: E:nth-of-type(n) { sRules } 说明: 匹配同类型中的第n个同级兄弟元素E.深圳dd马达 要使该属性生效,E元素必须是某个元素的子元素 ...
- 普通的java Ftp客户端的文件上传
关于ftp上传文件其实并不难,但有时候面对现实的环境还是很蛋疼的,今天我就分享一下,普通的上传文件文件至FTP的方式,它满足大部分FTP,但也有特别的,下篇博客会提及到. 下面我用一个FtpUtil, ...
- 学到了林海峰,武沛齐讲的Day14完
全局变量和局部变量 局部里面定义 global name ======将局部变量变成全局变量 nonlocal name # nonlocal,指定上一级变量,如果没有就继续往上直到找到为止 有 ...
- [GXOI/GZOI2019]特技飞行
题目链接 [https://www.luogu.org/problem/P5302] 思路:这道题可以说是两道题的合并.注意到\(c\)的分数与 \(a\)和\(b\)的分数 无关,也就是说可以分成两 ...
- 爬虫(四):BeautifulSoup库的使用
一:beautifulsoup简介 beautifulsoup是一个非常强大的工具,爬虫利器. beautifulSoup “美味的汤,绿色的浓汤” 一个灵活又方便的网页解析库,处理高效,支持多种解析 ...
- MySQL5.5安装步骤
原文链接:https://blog.csdn.net/m0_38025207/article/details/80723095 1.首先进入的是安装引导界面 2.然后进入的是类型选择界面,这里有3个类 ...
- 多层iframe取值问题
var fid = self.frameElement.getAttribute("id");//获取当前页面的iframe的id值 var fid = ‘workspace’: ...
- tez 0.9.0 配置
官网: https://tez.apache.org/releases/0.9.0/tez-api-javadocs/configs/TezConfiguration.html