Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2011 Alexander Kerner. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package net.sf.kerner.utils.impl.util;
 
 import java.io.File;
 
 public class UtilFileName {
 
     private UtilFileName() {
     }

    

Renames a file

Parameters:
file file that is renamed
newName new name for the file
keepExtension if true, file extension will be appended to new name, if there was any.
Returns:
true, if operation was successful, false otherwise
Throws:
java.io.FileNotFoundException
 
     public static boolean renameFile(File fileString newNameboolean keepExtensionthrows FileNotFoundException {
         synchronized (file) {
 
             if (!file.isFile()) {
                 throw new FileNotFoundException("cannot access file \"" + file + "\"");
             }
 
             final String fileNameExtension = getFileExtension(file);
 
             if (keepExtension) {
                 return file.renameTo(new File(file.getParentFile(), newName + fileNameExtension));
             } else {
                 return file.renameTo(new File(file.getParentFile(), newName));
             }
         }
     }

    

Given a File named "hans.txt". newName is "peter". Returning String will be "peter.txt".

Original name is "hans.txt.tex". newName is "peter". Returning String will be "peter.tex".

Original name is "hans.txt.tex". newName is "peter.txt". Returning String will be "peter.txt.tex".

Parameters:
file File, for which new name is wanted.
newName the new filename discarding extension.
Returns:
the new filename including extension.
 
     public static String getNewFileName(File fileString newName) {
         final String fileName = file.getName();
         final int posOfExt = fileName.lastIndexOf(".");
         if (posOfExt < 0) {
             return newName;
         }
         final String ext = fileName.substring(posOfExtfileName.length());
         return newName + ext;
     }
 
     public static File appendToFileName(File fileString string) {
         final String raw = getRawFileName(file);
         final String nameNew = new StringBuilder().append(raw).append(string).append(getFileExtension(file)).toString();
         final File result = new File(file.getParent(), nameNew);
         return result;
     }
 
     public static String getRawFileName(File file) {
         final String fileName = file.getName();
         final int posOfExt = fileName.lastIndexOf(".");
         if (posOfExt < 0) {
             return fileName;
         }
         return fileName.substring(0, posOfExt);
     }
 
    public static String getFileExtension(File file) {
        final String fileName = file.getName();
        final int posOfExt = fileName.lastIndexOf(".");
        if (posOfExt < 0) {
            return "";
        }
        return fileName.substring(posOfExtfileName.length());
    }
New to GrepCode? Check out our FAQ X