0

I'm using this code to get the absolute path of files inside a folder

public void addFiles(String fileFolder){
        ArrayList<String> files = new ArrayList<String>();     
        fileOp.getFiles(fileFolder, files);
}

But I want to get only the file name of the files (without extension). How can I do this?

Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38
Hyperion
  • 2,515
  • 11
  • 37
  • 59

4 Answers4

2

i don't think such a method exists. you can get the filename and get the last index of . and truncate the content after that and get the last index of File.separator and remove contents before that.

you got your file name.

or you can use FilenameUtils from apache commons IO and use the following

FilenameUtils.removeExtension(fileName);

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
0

There's a really good way to do this - you can use FilenameUtils.removeExtension.

Also, See: How to trim a file extension from a String

Community
  • 1
  • 1
0

This code will do the work of removing the extension and printing name of file:

      public static void main(String[] args) {
        String path = "C:\\Users\\abc\\some";
        File folder = new File(path);
        File[] files = folder.listFiles();
        String fileName;
        int lastPeriodPos;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                fileName = files[i].getName();
                lastPeriodPos = fileName.lastIndexOf('.');
                if (lastPeriodPos > 0)
                fileName = fileName.substring(0, lastPeriodPos);
                System.out.println("File name is " + fileName);
            }
        }
    }

If you are ok with standard libraries then use Apache Common as it has ready-made method for that.

akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
0
String filePath = "/storage/emulated/0/Android/data/myAppPackageName/files/Pictures/JPEG_20180813_124701_-894962406.jpg"


String nameWithoutExtension = Files.getNameWithoutExtension(filePath);
Jeff Padgett
  • 2,380
  • 22
  • 34