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.microedition.io.file.FileConnection 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.

 Example:
 
   void fileAccessExample(String path) {
      FileAdapter fa = new FileAdapter(path); // opens the FileConnection
      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();                             // closes the FileConnection
 


Constructor Summary
FileAdapter(java.lang.String path)
          Creates a FileAdapter instance, opening the underlying FileConnection in read/write mode.
FileAdapter(java.lang.String path, boolean readOnly)
          Creates a FileAdapter instance, opening the underlying FileConnection.
 
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.
 
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
Creates a FileAdapter instance, opening the underlying FileConnection in read/write mode.

Throws:
java.io.IOException

FileAdapter

public FileAdapter(java.lang.String path,
                   boolean readOnly)
            throws java.io.IOException
Creates a FileAdapter instance, opening the underlying FileConnection.

Parameters:
path - the file path
readOnly - true if file shall be opened in read only mode
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.

Parameters:
append - if TRUE, open the file for appending, otherwise the file is truncated to the beginning.
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

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.

getName

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


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. This is an empty implementation as JME does not support this operation. 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 cannot be performed (in this implementation this is always the case)
See Also:
isSetLastModifiedSupported.


Copyright © 2001-2009 Funambol.