com.funambol.platform
Class FileAdapter

java.lang.Object
  extended by com.funambol.platform.FileAdapter

public class FileAdapter
extends java.lang.Object

This class is a proxy to javax.io.File to provide a common access to a file resource on all mobile platforms. A portable code must use this class only to access files, and must take care of closing the FileAdapter when not used anymore, even this makes no action on Standard Edition.

 Example:
 
   void fileAccessExample(String path) {
      FileAdapter fa = new FileAdapter(path); // opens the File
      InputStream is = fa.openInputStream();  // opens the InputStream
      while( (char c = is.read()) != -1) {    // read till the end of the file
         System.out.print(c);
      }
      is.close();                             // closes the InputStream
      fa.close();     // * MUST DO, even if it does nothing on JavaSE *
 


Constructor Summary
FileAdapter(java.lang.String path)
          Construct a new FileAdapter, opening the underlying File.
FileAdapter(java.lang.String path, boolean readonly)
          Construct a new FileAdapter, opening the underlying File.
 
Method Summary
 void close()
          Close this FileAdapter
 void create()
          Create a file with the name of this FileAdapter.
 void delete()
          Delete the file with the name of this FileAdapter.
 boolean exists()
          Check if the file with the name of this FileAdapter exists.
 java.lang.String getName()
          Returns the name of a file or directory excluding the URL schema and all paths.
 long getSize()
          Check if the file with the name of this FileAdapter exists.
 boolean isDirectory()
          Returns if this FileAdapter represents a directory on the underlying file system.
 boolean isSetLastModifiedSupported()
          Returns true if the underlying platform supports the setLastModified method.
 long lastModified()
          Returns the timestamp of the last modification to the file
 java.util.Enumeration list(boolean includeSubdirs)
          Gets a list of all visible files and directories contained in a directory.
 void mkdir()
          Creates a directory corresponding to the directory string provided in the constructor.
 java.io.InputStream openInputStream()
          Open and return an input stream for this FileHandler.
 java.io.OutputStream openOutputStream()
          Open and return an output stream for this FileHandler.
 java.io.OutputStream openOutputStream(boolean append)
          Open and return an output stream for this FileHandler.
 void rename(java.lang.String newName)
          Renames this File to the name represented by the File dest.
 void setLastModified(long date)
          Sets the last modification time for this file Users shall check if the operation is supported by invoking
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileAdapter

public FileAdapter(java.lang.String path)
            throws java.io.IOException
Construct a new FileAdapter, opening the underlying File.

Throws:
java.io.IOException

FileAdapter

public FileAdapter(java.lang.String path,
                   boolean readonly)
            throws java.io.IOException
Construct a new FileAdapter, opening the underlying File.

Parameters:
readonly - open the file in readonly mode. This is currently ignored and the file is always opened in read/write
Throws:
java.io.IOException
Method Detail

openInputStream

public java.io.InputStream openInputStream()
                                    throws java.io.IOException
Open and return an input stream for this FileHandler. If the file does not exist, en IOException is thrown.

Throws:
java.io.IOException

openOutputStream

public java.io.OutputStream openOutputStream()
                                      throws java.io.IOException
Open and return an output stream for this FileHandler. If the file does not exist, it is created by this call, otherwise it is truncated to the beginning.

Throws:
java.io.IOException

openOutputStream

public java.io.OutputStream openOutputStream(boolean append)
                                      throws java.io.IOException
Open and return an output stream for this FileHandler. If the file does not exist, it is created by this call. The parameter append determines whether or not the file is opened and appended to or just opened empty.

Parameters:
append - a boolean indicating whether or not to append to an existing file.
Returns:
the new OutputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close this FileAdapter

Throws:
java.io.IOException

create

public void create()
            throws java.io.IOException
Create a file with the name of this FileAdapter.

Throws:
java.io.IOException

delete

public void delete()
            throws java.io.IOException
Delete the file with the name of this FileAdapter.

Throws:
java.io.IOException

rename

public void rename(java.lang.String newName)
            throws java.io.IOException
Renames this File to the name represented by the File dest. This works for both normal files and directories.

Parameters:
newName - - the File containing the new name.
Throws:
java.io.IOException

exists

public boolean exists()
               throws java.io.IOException
Check if the file with the name of this FileAdapter exists.

Throws:
java.io.IOException

getSize

public long getSize()
             throws java.io.IOException
Check if the file with the name of this FileAdapter exists.

Throws:
java.io.IOException

isDirectory

public boolean isDirectory()
                    throws java.io.IOException
Returns if this FileAdapter represents a directory on the underlying file system.

Throws:
java.io.IOException

list

public java.util.Enumeration list(boolean includeSubdirs)
                           throws java.io.IOException
Gets a list of all visible files and directories contained in a directory. The directory is the connection's target as specified in Connector.open().

Returns:
An Enumeration of strings, denoting the files and directories in the directory. The string returned contain only the file or directory name and does not contain any path prefix (to get a complete path for each file or directory, prepend getPath()). Directories are denoted with a trailing slash "/" in their returned name. The Enumeration has zero length if the directory is empty. Any hidden files and directories in the directory are not included in the returned list. Any current directory indication (".") and any parent directory indication ("..") is not included in the list of files and directories returned.
Throws:
java.io.IOException

getName

public java.lang.String getName()
Returns the name of a file or directory excluding the URL schema and all paths.


mkdir

public void mkdir()
           throws java.io.IOException
Creates a directory corresponding to the directory string provided in the constructor.

Throws:
java.io.IOException - if the directory cannot be created.

lastModified

public long lastModified()
Returns the timestamp of the last modification to the file


isSetLastModifiedSupported

public boolean isSetLastModifiedSupported()
Returns true if the underlying platform supports the setLastModified method.


setLastModified

public void setLastModified(long date)
                     throws java.io.IOException
Sets the last modification time for this file Users shall check if the operation is supported by invoking

Parameters:
date - the modification time expressed as UTC
Throws:
java.io.IOException - if the operation fails
See Also:
isSetLastModifiedSupported.


Copyright © 2001-2009 Funambol.