LoginSignup
3
0

More than 3 years have passed since last update.

【JAVA】拡張子を除いて、ファイル名のみを取得する

Posted at

目的

拡張子を除いて、ファイル名のみを取得する

実装例

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();

getName()メソッドでファイル名を取得する

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

substring()メソッドで第一引数から第二引数文字目までを取得
lastINdexOf()メソッドで末尾から数えて何番目に引数(’.’)があるのか調べる

これでabcが表示されるはず

参考文献

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

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0