[英]How to read all txt files in a given directory to look for a string? Java
[英]Java - Apply Function to All Files in Given Directory
我想知道是否可以使用Java将函数应用于目录中的所有文件?
更具体地说,我希望读取文件夹中的所有图像,将它们转换为二进制(两种颜色/黑白),然后将二进制图像保存为另一个目录中的新图像(例如, directory\\binary
) 。
像这样的东西:
for(all images in folder)
read in image
make(binaryImg)
saveNewImage
endFor
我有300张图片所以希望有办法做到这一点?
非常感谢。
你可以这样做:
File dir = new File("/dir/");
for(File f : dir.listFiles())
processImage(f);
当然,由于您有许多文件,您可以为每个文件创建一个Runnable
,并将它们传递给ExecutorService
以在多个线程中处理:
public static void main(String... args){
ExecutorService pool = Executors.newFixedThreadPool(4); //nbr of active threads
File dir = new File("/dir/");
for(File f : dir.listFiles())
pool.submit(new Runnable() {
public void run(){
processImage(f);
}
}
}
是的,这是可能的。 查看java.io.File并尝试搜索有关使用Java中的文件的主题,以获取有关如何访问目录以及获取其包含的文件列表的信息。
如果你正在寻找某人为你编写代码,那不太可能发生。
我能想到的唯一“更好的方法”是在这里使用多线程设计。 例如,所有100个图像都应该在单独的线程中处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.