[JAVA] Get only the file name, excluding the extension

Purpose

Get only the file name, excluding the extension

Implementation example

example.java


import java.io.File;
String fullPathString = "/home/hoge/abc.txt";

public static String getFileName(final String fullPathString) {
		File file = new File(fullPathString);
		String basename = file.getName();
		String woext = basename.substring(0,basename.lastIndexOf('.'));
		System.out.println(woext);
	}
String basename = file.getName();

Get the file name with the getName () method

String woext = basename.substring(0,basename.lastIndexOf('.'));

Get from the first argument to the second argument character with the substring () method Check the number of the argument ('.') Counting from the end with the lastINdexOf () method

You should now see abc

References

https://java-code.jp/187 https://java-code.jp/795 http://simplesandsamples.com/basename-rmext.java.html

Recommended Posts

[JAVA] Get only the file name, excluding the extension
[Java8] Search the directory and get the file
[Java] How to extract the file name from the path
[Java] Get the file in the jar regardless of the environment
[Java] Get the file path in the folder with List
I want to get a list of only unique character strings by excluding fixed character strings from the file name
How to get the class name / method name running in Java
Unzip the zip file in Java
Get the public URL of a private Flickr file in Java
Overwrite upload of file with the same name with BOX SDK (java)
How to get the length of an audio file in java
[Java] How to use the File class
[Java] Get the day of the specific day of the week
Get the column name from the Model instance
What is the best file reading (Java)
[Java] How to get the current directory
Compare Android Java dates only (excluding time)
How to get the date in java
I want to get the field name of the [Java] field. (Old tale tone)
Java: Place the ResourceBundle properties file anywhere
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
[Java] Get the date with the LocalDateTime class
JAVA: jar, aar, view the contents of the file
When you get lost in the class name
[Java] Get the length of the surrogate pair string
Rename the package name of the existing jar file
[Java] Read the file in src / main / resources
[Java] How to get random numbers excluding specific numbers
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to save a file with the specified extension under the directory specified in Java to the list
java file creation
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
[Kotlin] Get the argument name of the constructor by reflection
Get the anime name for this term by scraping
Get the name of the test case in the JUnit test class
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
[Java] Get the date 10 days later with the Calendar class
Ruby, Nokogiri: Get the element name of the selected node
Replace only part of the URL host with java
Get started with serverless Java with the lightweight framework Micronaut!