搜索目录中的文件并根据公共子字符串将它们配对 [英] Searching files in a directory and pairing them based on a common sub-string

查看:63
本文介绍了搜索目录中的文件并根据公共子字符串将它们配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为 ImageJ 编写一个解决方案来处理我的图像.

I have been attempting to program a solution for ImageJ to process my images.

我了解如何获取目录、在其上运行命令等.但是我遇到了一种情况,我现在需要开始使用某种类型的搜索功能,以便将两个图像在一个完整的目录中配对图像对.

I understand how to get a directory, run commands on it, etc etc. However I've run into a situation where I now need to start using some type of search function in order to pair two images together in a directory full of image pairs.

我希望你们能确认我的方向是正确的,我的想法是正确的.到目前为止,事实证明我很难理解,因为我使用 Java 的经验还不到一个月.由于这个项目直接用于我的研究,我确实有足够的动力来完成它,我只是需要一些对我有用的功能的指导.

I'm hoping that you guys can confirm I am on the right direction and that my idea is right. So far it is proving difficult for me to understand as I have less than even a month's worth of experience with Java. Being that this project is directly for my research I really do have plenty of drive to get it done I just need some direction in what functions are useful to me.

我最初想使用正则表达式,但我看到当你开始处理大量图像时(尤其是 imagej,它似乎不能很好地转储数据使用,如果这是正确的说法)正则表达式很慢.

I initially thought of using regex but I saw that when you start processing a lot of images (especially with imagej which it seems does not dump data usage well, if that's the correct way to say it) that regex is very slow.

这些图片的一般格式是:

The general format of these images is:

  • someString_DAPI_0001.tif
  • someString_GFP_0001.tif
  • someString_DAPI_0002.tif
  • someString_GFP_0002.tif
  • someString_DAPI_0003.tif
  • someString_GFP_0003.tif

它们按字母顺序排列,因此它应该能够转到列表中的下一个图像.我对我应该使用哪些功能来完成这个有点迷茫,但我认为我的整体结构是正确的.感谢 Java 论坛的一些帮助.但是我仍然坚持下一步要去哪里.

They are in alphabetical order so it should be able to go to the next image in the list. I'm just a bit lost on what functions I should use to accomplish this but I think my overall while structure is correct. Thanks to some help from Java forums. However I'm still stuck on where to go to next.

到目前为止,这是我的代码:感谢 这个 SO 部分代码的答案

So far here is my code: Thanks to this SO answer for partial code

int count = 0;
getFile("C:\");

string DAPI;
string GFP;


private void getFile(String dirPath) {
    File f = new File(dirPath);
    File[] files = f.listFiles();

    while (files.length > 0) {
        if (/* File name contains "DAPI"*/){
            DAPI = File f;
            string substitute to get 'GFP' filename
            store GFP file name into variable
            do something(DAPI, GFP);
        }
        advance to next filename in list
    }
}

截至目前,我真的不知道如何在字符串中搜索字符串.我见过正则表达式捕获组和其他解决方案,但我不知道处理数百张图像的最佳"解决方案.

As of right now I don't really know how to search for a string within a string. I've seen regex capture groups, and other solutions but I do not know the "best" one for processing hundreds of images.

我也不知道用什么函数来替换子字符串.

I also have no clue what function would be used to substitute substrings.

如果你们能指出最适合这种情况的功能,我将不胜感激.我喜欢弄清楚如何自己制作我只需要帮助获取正确的信息.还想确保我没有在这里犯重大的逻辑错误.

I'd much appreciate it if you guys could point me towards the functions best for this case. I like to figure out how to make it on my own I just need help getting to the right information. Also want to make sure I am not making major logic mistakes here.

推荐答案

如果您的文件名遵循您提到的简单模式,您似乎不需要正则表达式.您可以简单地遍历文件并根据文件名是否包含 DAPI 进行过滤,例如见下文.此代码可能过于简化了您的要求,但根据您提供的详细信息,我无法判断.

It doesn't seem like you need regex if your file names follow the simple pattern that you mentioned. You can simply iterate over the files and filter based on whether the filename contains DAPI e.g. see below. This code may be oversimplification of your requirements but I couldn't tell that based on the details you've provided.

import java.io.*;


public class Temp {

  int count = 0;

  private void getFile(String dirPath) {
    File f = new File(dirPath);
    File[] files = f.listFiles();

    if (files != null) {
      for (File file : files) {
        if (file.getName().contains("DAPI")) {
          String dapiFile = file.getName();
          String gfpFile = dapiFile.replace("DAPI", "GFP");
          doSomething(dapiFile, gfpFile);
        }
      }
    }
  }

  //Do Something does nothing right now, expand on it.
  private void doSomething(String dapiFile, String gfpFile) {
    System.out.println(new File(dapiFile).getAbsolutePath());
    System.out.println(new File(gfpFile).getAbsolutePath());
  }

  public static void main(String[] args) {
    Temp app = new Temp();
    app.getFile("C:\\tmp\\");
  }

}

注意:根据 Vogel612 的回答,如果您有 Java 8 并且喜欢功能性解决方案,您可以拥有:

NOTE: As per Vogel612's answer, if you have Java 8 and like a functional solution you can have:

private void getFile(String dirPath) {
  try {
    Files.find(Paths.get(dirPath), 1, (path, basicFileAttributes) -> (path.toFile().getName().contains("DAPI"))).forEach(
      dapiPath -> {
        Path gfpPath = dapiPath.resolveSibling(dapiPath.getFileName().toString().replace("DAPI", "GFP"));
        doSomething(dapiPath, gfpPath);
      });
  } catch (IOException e) {
    e.printStackTrace();
  }
}

//Dummy method does nothing yet.
private void doSomething(Path dapiPath, Path gfpPath) {
  System.out.println(dapiPath.toAbsolutePath().toString());
  System.out.println(gfpPath.toAbsolutePath().toString());
}

这篇关于搜索目录中的文件并根据公共子字符串将它们配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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