File Name Extension Utils : File « File « Android






File Name Extension Utils

    
//package com.akjava.lib.android.util;

import java.io.File;

import android.net.Uri;

/**
 * 
 * TODO merge to jakarta-commons-io filenameutils
 * 
 * (c)2009 aki akjava.com
 *
   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.
   
 *
 */

public class FileNameUtils {
  public static String getExtension(File file){
    return getExtension(file.getName());
  }
  
  public static String getExtension(String name){
    String ext;
    if(name.lastIndexOf(".")==-1){
      ext="";
    
    }else{
      int index=name.lastIndexOf(".");
      ext=name.substring(index+1,name.length());
    }
    return ext;
  }
  
  public static String getRemovedExtensionName(String name){
    String baseName;
    if(name.lastIndexOf(".")==-1){
      baseName=name;
    
    }else{
      int index=name.lastIndexOf(".");
      baseName=name.substring(0,index);
    }
    return baseName;
  }
  
  public static File getChangedExtensionFile(File file,String extension,boolean overwrite){
    File newFile=null;
    String baseName=null;
    if(file.getName().lastIndexOf(".")==-1){
      baseName=file.getName();
    
    }else{
      int index=file.getName().lastIndexOf(".");
      baseName=file.getName().substring(0,index);
    }
    String bName=baseName;
    if(!extension.equals("")){
      bName+="."+extension;
    }
    newFile=new File(file.getParent(),bName);
    int index=1;
    
    
    
    if(!overwrite){
    while(newFile.exists()){
      String specific="("+index+")";
      String tmpName=baseName+specific;
      if(!extension.equals("")){
        tmpName+="."+extension;
        }
      newFile=new File(file.getParent(),tmpName);
      index++;
    }
    }
    return newFile;
  }
  public static final String SCHME_FILE="file";
  public static final String SCHME_FILE_AND_SEPARATOR="file://";
  public static String uriToPath(Uri uri){
    if(uri.getScheme().equals(SCHME_FILE)){
           return uri.getPath();
           }
    return null;
  }
  public static Uri pathToUri(String path){
    return Uri.parse(SCHME_FILE_AND_SEPARATOR+path);
  }

  public static boolean isFileUri(String uri){
    return uri!=null && uri.startsWith(SCHME_FILE_AND_SEPARATOR);
  }
  public static String uriToPath(String uri) {
    if(isFileUri(uri)){
      return uri.substring(SCHME_FILE_AND_SEPARATOR.length());
    }
    return null;
  }
}

   
    
    
    
  








Related examples in the same category

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Save to Android local file system
10.Using Ini file to manage Preferences
11.File Cache
12.Format File Size
13.File Upload
14.Write and append string to file
15.Transfers required files to the the private file system part in order to be access them from C Code.
16.returns a canonical line of a obj or mtl file.
17.Trims down obj files, so that they may be parsed faster later on.
18.Format File Size:KB, MB, GB
19.Get File Contents as String
20.Read File and return CharSequence
21.Copy File
22.Read/write From FileSystem, browse Account
23.Create Path and file under External Storage
24.Get File Extension Name
25.Extract File Name From String
26.Write String to file
27.Create an output file from raw resources.
28.Copies a directory from a jar file to an external directory.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.