Android File Extension Name Get changeExtension(File file, String extensionNew)

Here you can find the source of changeExtension(File file, String extensionNew)

Description

Trys to change file's extension to extensionNew.

License

Open Source License

Parameter

Parameter Description
file the currently existing file
extensionNew the new extension for file; must not contain a '.' char in it

Exception

Parameter Description
IllegalArgumentException if file is Check#validFile not valid; extensionNew is blank or contains a '.' char
SecurityException if a security manager exists and write access to file
IOException if an I/O problem occurs; this includes failure of the file to be renamed

Return

a new File instance that has extensionNew

Declaration

public static File changeExtension(File file, String extensionNew)
        throws IllegalArgumentException, SecurityException, IOException 

Method Source Code

/*//from   www.j a  v  a  2s.c o m
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Trys to change file's extension to extensionNew.
     * If file currently has an extension, it is replaced by extensionNew,
     * and if file lacks an extension, extensionNew is appended to its name.
     * The new file must not currently exist (to be safest, this method never overwrites).
     * <p>
     * @param file the currently existing file
     * @param extensionNew the new extension for file; must not contain a '.' char in it
     * @return a new File instance that has extensionNew
     * @throws IllegalArgumentException if file is {@link Check#validFile not valid}; extensionNew is blank or contains a '.' char
     * @throws SecurityException if a security manager exists and write access to file
     * @throws IOException if an I/O problem occurs; this includes failure of the file to be renamed
     */
    public static File changeExtension(File file, String extensionNew)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().validFile(file);
        Check.arg().notBlank(extensionNew);
        if (extensionNew.contains("."))
            throw new IllegalArgumentException("extensionNew = "
                    + extensionNew + " contains a '.' char");

        File parent = file.getParentFile();
        String nameNew = getNameMinusExtension(file) + "." + extensionNew;
        File fileNew = new File(parent, nameNew);
        return rename(file, fileNew);
    }
    /**
     * Returns the file's name minus any extension, that is, the part of its name up to (but not including) the last '.' char.
     * For example, if presented with a file named "helloWorld.old.txt" then "helloWorld.old" is returned.
     * If no extension exists (either because there is nothing after the last '.' char or a '.' char never occurs)
     * then the complete file name (up to but not including any final '.' char) is returned.
     * Note that the File need not actually exist nor be a normal file.
     * <p>
     * @param file the File whose name is will be returned
     * @throws IllegalArgumentException if file == null
     */
    public static String getNameMinusExtension(File file)
            throws IllegalArgumentException {
        Check.arg().notNull(file);

        String name = file.getName();
        int indexPeriod = name.lastIndexOf('.');
        if (indexPeriod == -1)
            return name;
        else
            return name.substring(0, indexPeriod);
    }
    /**
     * Trys to rename file1 to file2.
     * Since file2 may be in a different directory and/or have a different name,
     * this is really a combined move/rename method.
     * <p>
     * This method was written because {@link File#renameTo File.renameTo} unfortunately
     * merely returns a boolean indicating the success of the operation, which forces the user to check.
     * In contrast, this method corrects that defect and throws an Exception instead.
     * <p>
     * Furthermore, for maximum safety, this method will never overwrite an existing but different file.
     * Therefore, it insists that file2 must not currently exist unless it is equal to file1.
     * (One reason why the user may wish to supply file2 equal to file1 is if their
     * operating system has case insensitive file names:
     * perhaps they are simply trying to change file1's name to a standard case.)
     * <p>
     * Note: {@link DirUtil#rename DirUtil.rename} should be used to rename directories.
     * <p>
     * @param file1 the currently existing file
     * @param file2 the file that is to be renamed to
     * @return file2
     * @throws IllegalArgumentException if file1 is {@link Check#validFile not valid};
     * file2 == null; file2 already exists and is not equal to file1
     * @throws SecurityException if a security manager exists and denies write access to file1 or file2
     * @throws IOException if an I/O problem occurs; this includes failure of the file to be renamed
     */
    public static File rename(File file1, File file2)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().validFile(file1);
        Check.arg().notNull(file2);
        if (file2.exists() && !file1.equals(file2))
            throw new IllegalArgumentException("file2 = " + file2.getPath()
                    + " already exists and is not equal to file1 = "
                    + file1.getPath());

        boolean renamed = file1.renameTo(file2);
        if (!renamed)
            throw new IOException("failed to rename file1 = "
                    + file1.getPath() + " to file2 = " + file2.getPath());
        return file2;
    }
}

Related

  1. getExtension(File file)
  2. getExtension(File file)
  3. getExtension(File file, boolean useFirstPeriod, boolean includePeriod)
  4. getExtension(String file)