首页 \ 问答 \ 带有groovy的DirectoryStream(DirectoryStream with groovy)

带有groovy的DirectoryStream(DirectoryStream with groovy)

我想使用java 8 DirectoryStream来查找与glob模式匹配的文件,但我想在Groovy中完成(至少2.4)。 我很难找到一个如何做到这一点的例子,因为在groovy中不存在try-with-resources。

另外,如果搜索模式是** / * .txt,该怎么办? 该模式表明它应该跨越目录边界,但我对DirectoryStream的理解是它没有。

def recent = {File file -> new Date() - new Date(file.lastModified) < 7}
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, job.pattern)) {
                    for (Path entry : stream) {
                            if(recent){
                                /*dostuff*/
                            }       
                        }
                    }

I want to use a java 8 DirectoryStream to find files that match a glob pattern, but I want to do it in Groovy (2.4 at least). I'm having trouble finding an example of how to do it though, since try-with-resources doesn't exist in groovy.

Additionally, what if the search pattern is **/*.txt. The pattern says it should cross directory boundaries, but my understanding of the DirectoryStream is that it doesn't.

def recent = {File file -> new Date() - new Date(file.lastModified) < 7}
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, job.pattern)) {
                    for (Path entry : stream) {
                            if(recent){
                                /*dostuff*/
                            }       
                        }
                    }

原文:https://stackoverflow.com/questions/43459697
更新时间:2024-01-31 08:01

最满意答案

sem_open的Linux手册页说(重点补充):

ENOENT在oflag中未指定O_CREAT标志,并且不存在具有此名称的信号量; 或者,指定了O_CREAT但名称不是很好

sem_overview说明了这个信号量名称:

命名信号量由表单名称/somename ; 也就是说,一个以空字符结尾的字符串,最多NAME_MAX-4 (即251个)字符,包含一个初始斜杠,后跟一个或多个字符,其中没有一个是斜杠。


The Linux manpage for sem_open says (emphasis added):

ENOENT The O_CREAT flag was not specified in oflag and no semaphore with this name exists; or, O_CREAT was specified, but name wasn't well formed.

The sem_overview says this about semaphore names:

A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes.

相关问答

更多
  • 我想你在问, “我如何解决OS X上缺少sem_getvalue() ?” 我可以想到三种方法: 首先(在我看来,最好)重新设计你的程序,以便信号量的当前值永远不需要。 毕竟, 正如文档警告 , sem_getvalue报告的值在收到时不一定准确。 其次,如果有必要,包装POSIX信号量函数并保留自己的计数。 每个sem_t都可以用一个计数器和一个保护该计数器的互斥体来装饰。 你的实现可能会(并且可能应该有!)与sem_getvalue相同的警告, sem_getvalue ,一旦检索到计数就不可信。 第三 ...
  • 如sem_open手册页中所述 : 信号量由名称标识。 由于您的代码为sem_open调用提供了相同的名称值( /mysem ),因此导致引用full和empty的相同信号量。 这显然不是该计划的逻辑应该是什么。 相反,为每个信号打开不同的信号量。 检查所有函数调用的返回值也是最佳做法。 empty = sem_open("/empty_sem", O_CREAT, 0644, BUFFER_SIZE); if (empty == SEM_FAILED) { perror("Failed to o ...
  • 使用PHP exec("whoami"); 确定用户,很可能是www-data 。 这将让您了解apache尝试访问系统资源的用户。 然后,您需要启用此用户才能访问这些资源。 此用户通常位于: /etc/apache2/envvars Use PHP exec("whoami"); to determine the user, it is likely www-data. This will let you know the user with which apache is trying to acces ...
  • 我需要同时使用sem_close和sem_unlink 。 在OS X的sem_init中提到了这一点,但我错过了重要性。 这个答案有助于详细说明何时使用每个功能。 总结一下: sem_close只释放信号量使用的资源。 封闭的信号量持续存在,可以重新打开。 sem_unlink标记当所有进程停止使用它时要销毁的信号量。 正如@JohnBollinger在评论中补充的那样, 如果您只需要在一个程序的一次运行期间使用信号量,那么您应该考虑在创建它之后立即取消链接(通过sem_unlink())。 然后,您可以 ...
  • 虽然管理员可以根据需要设置下限,但Linux范围内没有20个限制。 专用服务器可以有100k +甚至1M +并发TCP连接,每个连接需要一个开放的FD。 但是,有一些系统调用会返回更有限的范围,例如reboot和stat ,它们总是返回0或-1 。 为什么那些返回int而不是16位短路甚至8位字符? 有几个原因(这里概述为32位x86 Linux): 为了使编写系统库和编译器更容易,x86 32位Linux有一个约定,所有系统调用都返回32位寄存器eax的值。 没有例外。 glibc的open()是一个在实 ...
  • sem_open()创建的信号量是一个命名信号量。 命名信号量的基本目的是在不相关的进程之间使用。 sem_init()创建的信号量是一个未命名的信号量。 它比命名信号量重量轻,如果在相关进程之间使用,则需要放在共享内存中。 如果在同一进程的线程之间使用,则可以将其保存在全局变量中。 sem_open()返回的指针实际上是指向由mmap()映射的内存的指针,并设置了MAP_SHARED标志。 由于这种类型的内存在fork()持续存在,因此您可以在父级和子级中使用相同的变量来访问命名的信号量。 The sem ...
  • Mac OSX不符合要求,不支持sem_init 。 该函数存在,但它无声地失败或更糟,让你有一个不工作的信号量。 我鼓励你向Apple提交一个错误,因为这是一个真正的,长期存在的问题,严重影响了应用程序的可移植性。 抱怨的人越多,就越有希望得到修复。 至于解决它,您可以尝试查找/编写所有POSIX信号量函数的替换实现并将程序链接到该函数,或者您可以切换到使用sem_open而不是mmap和sem_init 。 (只要你已经经历了为每个信号量映射整个页面的开销, sem_open实际上不会花费你更多的东西。 ...
  • 我认为你的问题在于访问key 。 无法保证对共享变量的访问是原子的。 特别是编译器可能优化从内存中读取值的值,这样它就不会看到在另一个线程中完成的值的修改。 为了避免优化器启动,您必须声明您的key变量volatile 。 但是我不确定你编写程序的方式会保证有内存障碍可以保证线程可以保证看到修改并且并发写入不会混淆。 现代C11还具有_Atomic以确保访问是原子的。 但是C11尚未完全实现(可能有一些编译器具有该功能)。 如果是这样,信号量就会出现问题,因为C11只有互斥锁而不是信号量作为锁结构。 尚未指 ...
  • sem_open的Linux手册页说(重点补充): ENOENT在oflag中未指定O_CREAT标志,并且不存在具有此名称的信号量; 或者,指定了O_CREAT , 但名称不是很好 。 sem_overview说明了这个信号量名称: 命名信号量由表单名称/somename ; 也就是说,一个以空字符结尾的字符串,最多NAME_MAX-4 (即251个)字符,包含一个初始斜杠,后跟一个或多个字符,其中没有一个是斜杠。 The Linux manpage for sem_open says (emphasis ...
  • 你想要编译选项-pthread (如果你真的使用pthreads)。 如果你只是需要这些函数,它们在librt所以使用-lrt You want the compile option -pthread (if you are really using pthreads). If you just need those functions they are in librt so use -lrt

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)