[JAVA] I want to get a list of only unique character strings by excluding fixed character strings from the file name


Note that when the above file was in the directory, I sometimes wanted to extract only a unique character string from the file name. I checked the sauce but didn't use it, so it's a memorial service

Operating environment

Java SE 8 (jdk1.8.0_121)

What i did


import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class GetFileName {
	public static void main(String[] args) {
		String fileLeadName = "hogehoge_"; //First character
		String fileEndName = "_hoge"; //Last character
		File file = new File("File directory path name");
		File files[] = file.listFiles();
		List<File> fileList = new ArrayList<File>();
		for(File fileAndDir : files) {
			//List only file names
		for(File originalFileName : fileList) {
			// getName()Stringed with
			String fileName = originalFileName.getName();
			//Determine if the first and last characters are included
			if(fileName.indexOf(leadFileName) != -1 && fileName.indexOf(endFileName) != -1) {
				// fileLeadName.indexOf(fileLeadName)Is 0, so omitted
				String fileUniqueName = fileName.substring(fileLeadName.length()



If the character string at the end is not a fixed character string such as a date, consider it separately.

If it is separated by "\ _" as above, use lastIndexOf (fileEndName) Is it possible to deal with it by setting it to lastIndexOf ('_')?

Recommended Posts

I want to get a list of only unique character strings by excluding fixed character strings from the file name
I want to get a list of the contents of a zip file and its uncompressed size
I want to get the field name of the [Java] field. (Old tale tone)
[JAVA] Get only the file name, excluding the extension
I want to get only the time from Time type data ...! [Strftime] * Additional notes
I want to recursively get the superclass and interface of a certain class
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
I want to display the name of the poster of the comment
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I want to give a class name to the select attribute
I want to limit the input by narrowing the range of numbers
I made a tool to output the difference of CSV file
I want to extract between character strings with a regular expression
[Android] I want to get the listener from the button in ListView
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
[Rails] Get a unique record from the table joined by the join method of Active Record (Rails Tutorial Chapter 14)
[Ruby] I want to output only the odd-numbered characters in the character string
[Ruby] I want to extract only the value of the hash and only the key
I want to find out what character the character string appears from the left
I want you to use Enum # name () for the Key of SharedPreference
[Ruby] I want to make an array from a character string with the split method. And vice versa.
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
What I tried when I wanted to get all the fields of a bean
I was swallowed by the darkness of the romaji, trying to convert my name to romaji
I managed to get a blank when I brought the contents of Beans to the textarea
I want to output the day of the week
I want to var_dump the contents of the intent
I want to get the value in Ruby
I want to output the character number from the left where an arbitrary character string appears.
[Ruby] I want to make a program that displays today's day of the week!
I want to go back to a specific VC by tapping the back button on the NavigationBar!
[Rails] I want to display the link destination of link_to in a separate tab
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
I want to call a method of another class
[Kotlin] Get the argument name of the constructor by reflection
[Java] How to extract the file name from the path
I want to know the answer of the rock-paper-scissors app
I want to monitor a specific file with WatchService
Extract a specific element from the list of objects
I learned about the existence of a gemspec file
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to connect to Heroku MySQL from a client
I want to add a delete function to the comment function
I want to find out which version of java the jar file I have is available
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I tried a puzzle that can only be solved by the bottom 10% of bad engineers
I want to recreate the contents of assets from scratch in the environment built with capistrano
I want to find out if the specified character string is supported by the target character code
Get the object name of the instance created by the new operator
Get to the abbreviations from 5 examples of iterating Java lists
How to connect the strings in the List separated by commas
I want to expand the clickable part of the link_to method
I want to make a specific model of ActiveRecord ReadOnly
I want to change the log output settings of UtilLoggingJdbcLogger
I want to make a list with kotlin and java!
I want to call a method and count the number