简体   繁体   中英

Java, Eclipse: Finding Files ending in a certain pattern/extension/whatever

So basically I am using Eclipse to program in java and I have a workspace where my project folder is. By default, when no directory is specified when writing a file (eg "./file.txt"), the file gets saved in the Project folder/directory.

Now, what I want to do is find a file or files in my project directory that ends in a certain parttern/extension/whatever (eg "le.txt"), which would probably return true or a list of the files names that end that way (depending on how the method is written) in which probably the file "file.txt" from the first example would be included.

I found this in the internet and was attempting to use it as a guideline to make my own code, but found a few problems hard to solve:

  1. The code includes directory, which in my case it doesn't, I just mean to use the default project directory. How to say to the program to directly look for files in the project directory?
  2. I see the code being very complicated. I remember a friend doing something similar and it wasn't as complicated as in the code in the link. Although this is not really all that important, it would be great if someone could tell me a simpler workaround to the way it's done in the link.

Thank you, and I hope this question doesn't sound too ambiguous despite that I have 2 questions...

How can I use the GUI search feature?

Search → File... or Ctrl - H .

在此处输入图片说明

You can filter using a file name pattern in the "File name patterns" input field.

The current directory is '.' (the dot). Do you want to search all subdirectories recursively, or are the file you want in a single directory? In the latter case a new File("./subdir").list() suffices. Also, do you want them packaged with your class files? If yes, consider putting them in a package and use this.getClass().getClassLoader().getResources().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM