只读有形项目directory's文件名 [英] Read only visibles directory´s file names

查看:109
本文介绍了只读有形项目directory's文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去阅读一个文件夹中的文件名的将它们保存在一个数组列表,但我得到了我的数组,我不想invisble文件名(其实我只想保存.txt文件名)。是否任何人知道HOR创建列表之前更改,在code?

 字符串pathLevel =/用户/ MaxRuizTagle /桌面/拉特/;档案文件=新的文件(pathLevel);
的String [] = levelNames的File.List();
的String [] =矩阵新的String [levelNames.length]


解决方案

使用java.nio.file。使用Java 8:

 最终路径DIR = Paths.get(/用户/ MaxRuizTagle /桌面/拉特/);最终名单<串GT; TEXTFILES = Files.list(DIR)
    .filter(路径 - >!Files.isHidden(路径))
    .MAP(路径 - 方式> path.getFileName()的toString())
    .filter(S - >中。TXTs.endsWith())
    .collect(Collectors.toList());

如果Java 7中,做<一个相当于href=\"http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#newDirectoryStream-java.nio.file.Path-\"相对=nofollow> Files.newDirectoryStream()

 最终路径DIR = Paths.get(/用户/ MaxRuizTagle /桌面/拉特/);最后DirectoryStream&LT;路径和GT; dirstream
    = Files.newDirectoryStream(DIR,的* .txt);最终名单&LT;串GT; TEXTFILES =新的ArrayList&LT;&GT;();对于(最终路径条目:dirstream)
    如果(!Files.isHidden(入门))
        textFiles.add(entry.getFileName()的toString());

Im trying to read the filenames of a folder an save them in an array list, but i get invisble files names in my array that i dont want(actually i want only to saves the .txt file names). Does anybody knows hor to change that in the code before creating the list?

String pathLevel= "/Users/MaxRuizTagle/Desktop/lvl/";

File file = new File(pathLevel);
String [] levelNames = file.list();


String [] matrix= new String[levelNames.length];

解决方案

Use java.nio.file. Using Java 8:

final Path dir = Paths.get("/Users/MaxRuizTagle/Desktop/lvl/");

final List<String> textFiles = Files.list(dir)
    .filter(path -> !Files.isHidden(path))
    .map(path -> path.getFileName().toString())
    .filter(s -> s.endsWith(".txt"))
    .collect(Collectors.toList());

If Java 7, do the equivalent with Files.newDirectoryStream():

final Path dir = Paths.get("/Users/MaxRuizTagle/Desktop/lvl/");

final DirectoryStream<Path> dirstream
    = Files.newDirectoryStream(dir, "*.txt");

final List<String> textFiles = new ArrayList<>();

for (final Path entry: dirstream)
    if (!Files.isHidden(entry))
        textFiles.add(entry.getFileName().toString());

这篇关于只读有形项目directory's文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆