我正在寻找一个库,它会提供一种方法,该方法会给我一个匹配给定类 Ant 模式的文件列表。
对于 *foo/**/*.txt 我会得到
foo/x.txt
foo/bar/baz/.txt
myfoo/baz/boo/bar.txt
等我知道这可以通过 DirWalker 和
PathMatcher mat = FileSystems.getDefault().getPathMatcher("glob:" + filesPattern);
,但我更喜欢一些维护的库。我希望 Commons IO 拥有它,但没有。
更新:我很高兴重用 Ant 的代码,但更喜欢比整个 Ant 更小的东西。
最佳答案
所以我为了速度牺牲了几MB的应用程序大小并使用Ant's DirectoryScanner最后。
此外,还有 Spring 的 PathMatchingResourcePatternResolver .
//files = new PatternDirWalker( filesPattern ).list( baseDir );
files = new DirScanner( filesPattern ).list( baseDir );
public class DirScanner {
private String pattern;
public DirScanner( String pattern ) {
this.pattern = pattern;
}
public List<File> list( File dirToScan ) throws IOException {
DirectoryScanner ds = new DirectoryScanner();
String[] includes = { this.pattern };
//String[] excludes = {"modules\\*\\**"};
ds.setIncludes(includes);
//ds.setExcludes(excludes);
ds.setBasedir( dirToScan );
//ds.setCaseSensitive(true);
ds.scan();
String[] matches = ds.getIncludedFiles();
List<File> files = new ArrayList(matches.length);
for (int i = 0; i < matches.length; i++) {
files.add( new File(matches[i]) );
}
return files;
}
}// class
这是我开始编写代码但未完成的提示,只是希望有人愿意完成它。这个想法是它会保留一堆模式,遍历目录树并将内容与实际堆栈深度进行比较,在 ** 的情况下将其余内容进行比较。
但我求助于 PathMatcher,然后求助于 Ant 的实现。
public class PatternDirWalker {
//private static final Logger log = LoggerFactory.getLogger( PatternDirWalker.class );
private String pattern;
private List segments;
private PathMatcher mat;
public PatternDirWalker( String pattern ) {
this.pattern = pattern;
this.segments = parseSegments(pattern);
this.mat = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
public List<File> list( File dirToScan ) throws IOException{
return new DirectoryWalker() {
List<File> files = new LinkedList();
@Override protected void handleFile( File file, int depth, Collection results ) throws IOException {
if( PatternDirWalker.this.mat.matches( file.toPath()) )
results.add( file );
}
public List<File> findMatchingFiles( File dirToWalk ) throws IOException {
this.walk( dirToWalk, this.files );
return this.files;
}
}.findMatchingFiles( dirToScan );
}// list()
private List<Segment> parseSegments( String pattern ) {
String[] parts = StringUtils.split("/", pattern);
List<Segment> segs = new ArrayList(parts.length);
for( String part : parts ) {
Segment seg = new Segment(part);
segs.add( seg );
}
return segs;
}
class Segment {
public final String pat; // TODO: Tokenize
private Segment( String pat ) {
this.pat = pat;
}
}
}// class
关于为 glob 或类似 Ant 的模式 "*foo/**/*.txt"返回 List<File> 的 Java 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16923874/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|