Java File Name Get getFileNameWithNewExtension(File parent, File file, String ext)

Here you can find the source of getFileNameWithNewExtension(File parent, File file, String ext)

Description

get File Name With New Extension

License

Open Source License

Declaration

public static File getFileNameWithNewExtension(File parent, File file, String ext) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static File getFileNameWithNewExtension(File parent, File file, String ext) {
        return isFileExists(new File(parent, file.getName()), ext);
    }//  ww w  .j  av  a2 s  .  com

    public static File isFileExists(String f, String ext) {
        return isFileExists(new File(f), ext);
    }

    public static File isFileExists(File f, String ext) {
        int point = f.getName().lastIndexOf('.');

        if (point == -1) {
            point = f.getName().length();
        }

        File lowerCasedFile = new File(f.getParentFile(),
                f.getName().substring(0, point) + "." + ext.toLowerCase());
        if (lowerCasedFile.exists()) {
            return lowerCasedFile;
        }

        File upperCasedFile = new File(f.getParentFile(),
                f.getName().substring(0, point) + "." + ext.toUpperCase());
        if (upperCasedFile.exists()) {
            return upperCasedFile;
        }

        return null;
    }
}

Related

  1. getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter, final int pMaxFiles)
  2. getFileNameStartingWith(String directory, String prefix)
  3. getFilenameSuffix(final File file)
  4. getFileNameUtil(String dataPath)
  5. getFileNameWithAddedExtension(File parent, File f, String ext)
  6. getFilenameWithoutAnyExtension(File file)
  7. getFileNameWithoutExt(File file)
  8. getFileNameWithoutExt(File filePath)
  9. getFileNameWithoutExt(String file)