Adaptation of the class File to add some usefull functions : File « File « Android






Adaptation of the class File to add some usefull functions

    
/*
 * This file is part of andExplorer, android.ovhoo.com
 * Copyright (C) 2007 Mohamed ZALIM <mzalim@ovhoo.com>
 *
 * andExplorer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * andExplorer 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
//package com.ovhoo.android.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import android.net.Uri;



/**
 * Adaptation of the class File to add some usefull functions
 * @author mzalim
 *
 */
public class FileInterface{
  
  public static enum fileSizeUnit {FILE_SIZE_BITS, FILE_SIZE_BYTES, FILE_SIZE_KIB, FILE_SIZE_MIB, FILE_SIZE_GIB, FILE_SIZE_TIB};
  
  
  private File file;
  
  public FileInterface(File file) {
    this.file = file;
  }
  
  public FileInterface(String  path) {
    this.file = new File(path);
  }
  
  
  public String getParentDir(){
    String _parent = this.file.getParent();
    if (_parent == null){
      return "/";
    }else{
      return _parent;
    }
    
  }
  
  public ArrayList<FileInterface> ls(){
    if (this.file.isDirectory()){
      ArrayList<FileInterface> _retour = new ArrayList<FileInterface>();
      java.io.File _files[] = this.file.listFiles();
      for(int _index=0; _index < _files.length; _index ++){
        _retour.add(new FileInterface(_files[_index]));
      }
      
      return _retour;
    }
    else{
      return null;
    }
  }
  
  public ArrayList<FileInterface> ls(FilenameFilter filter){
    if (this.file.isDirectory()){
      ArrayList<FileInterface> _retour = new ArrayList<FileInterface>();
      java.io.File _files[] = this.file.listFiles(filter);
      
      for(int _index=0; _index < _files.length; _index ++){
        _retour.add(new FileInterface(_files[_index]));
      }
      
      return _retour;
    }
    else{
      return null;
    }
  }
  
  /**
   * Returns the file size in human readable units
   * Defaut unit is in Bytes
   * @return file size as String
   */
  public String getFileSize(fileSizeUnit unit){
    long _size = this.file.length();
    if (unit == null){
      return _size + " Bytes";
    }
    
    switch(unit){
    case FILE_SIZE_BITS : return _size*8 + " Bits";
    case FILE_SIZE_KIB : return _size/1024 + " KIB";
    case FILE_SIZE_MIB : return _size/1048576 + " MIB";
    case FILE_SIZE_GIB : return _size/1073741824 + " GIB";
    
    default: return _size + " Bytes";
    }
    
  }
  
  /**
   * Returns the file size in human readable units
   * calculate the best unit to use
   */
  public String getFileSize(){
    long _size = this.file.length();
    
    if (_size>2073741824){
       return _size/1073741824 + " GIB";
    }
    else if (_size>10485760){
      return _size/1048576 + " MIB";
    }
    else if (_size>10240){
      return _size/1024 + " KIB";
    }
    else if (_size>10){
      return _size + " Bytes";
    }
    else{
      return _size*8 + " Bits";
    }
  }
  
  public String getName(){
    return this.file.getName();
  }
  
  public boolean isDirectory(){
    return this.file.isDirectory();
  }
  
  public String getPath(){
    return this.file.getPath();
  }
  
  public String getAbsolutePath(){
    return this.file.getAbsolutePath();
  }
  
  public boolean mkdirs(){
    return this.file.mkdirs();
  }

  public boolean renameTo(String name) {
    File _file= new File(this.getParentDir() + "/" + name);
    return this.file.renameTo(_file);
  }

  public boolean delete() {
    return this.file.delete();
  }

  public Uri getUri() {
    return Uri.parse("file://"+ this.file.getAbsolutePath());
  }
  
  
  public boolean equals(Object obj){
    if (obj instanceof FileInterface){
      return this.file.equals(((FileInterface)obj).file);
    }
    else{
      return false;
    }
    
    
  }

  public File getFile() {
    return this.file;
  }
}

   
    
    
    
  








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.Save to Android local file system
9.Using Ini file to manage Preferences
10.File Cache
11.Format File Size
12.File Upload
13.File Name Extension Utils
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.