waba.io
Class File

java.lang.Object
  |
  +--waba.io.Stream
        |
        +--waba.io.File

public class File
extends Stream

File is a file or directory.

The File class will not work under the PalmPilot since it does not contain a filesystem.

Here is an example showing data being read from a file:

 File file = new File("/temp/tempfile", File.READ_ONLY);
 if (!file.isOpen())
   return;
 byte b[] = new byte[10];
 file.readBytes(b, 0, 10);
 file.close();
 file = new File("/temp/tempfile", File.DONT_OPEN);
 file.delete();
 


Field Summary
static int CREATE
          Create open mode.
static int DONT_OPEN
          Don't open the file mode.
static int READ_ONLY
          Read-only open mode.
static int READ_WRITE
          Read-write open mode.
static int WRITE_ONLY
          Write-only open mode.
 
Constructor Summary
File(java.lang.String path, int mode)
          Opens a file with the given name and mode.
 
Method Summary
 boolean close()
          Closes the file.
 boolean createDir()
          Creates a directory.
 boolean delete()
          Deletes the file or directory.
 boolean exists()
          Returns true if the file exists and false otherwise.
 int getLength()
          Returns the length of the file in bytes.
 java.lang.String getPath()
          Return the file's path.
 boolean isDir()
          Returns true if the file is a directory and false otherwise.
 boolean isOpen()
          Returns true if the file is open for reading or writing and false otherwise.
 java.lang.String[] listDir()
          Lists the files contained in a directory.
 int readBytes(byte[] b, int off, int len)
          Reads bytes from the file into a byte array.
 boolean rename(java.lang.String path)
          Renames the file.
 boolean seek(int pos)
          Sets the file pointer for read and write operations to the given position.
 int writeBytes(byte[] b, int off, int len)
          Writes to the file.
 
Methods inherited from class java.lang.Object
hashCode, toString
 

Field Detail

DONT_OPEN

public static final int DONT_OPEN
Don't open the file mode.

READ_ONLY

public static final int READ_ONLY
Read-only open mode.

WRITE_ONLY

public static final int WRITE_ONLY
Write-only open mode.

READ_WRITE

public static final int READ_WRITE
Read-write open mode.

CREATE

public static final int CREATE
Create open mode. Used to create a file if one does not exist.
Constructor Detail

File

public File(java.lang.String path,
            int mode)
Opens a file with the given name and mode. If mode is CREATE, the file will be created if it does not exist. The DONT_OPEN mode allows the exists(), rename(), delete(), listDir(), createDir() and isDir() methods to be called without requiring the file to be open for reading or writing.
Parameters:
path - the file's path
mode - one of DONT_OPEN, READ_ONLY, WRITE_ONLY, READ_WRITE or CREATE
Method Detail

close

public boolean close()
Closes the file. Returns true if the operation is successful and false otherwise.
Overrides:
close in class Stream

isOpen

public boolean isOpen()
Returns true if the file is open for reading or writing and false otherwise. This can be used to check if opening or creating a file was successful.

createDir

public boolean createDir()
Creates a directory. Returns true if the operation is successful and false otherwise.

delete

public boolean delete()
Deletes the file or directory. Returns true if the operation is successful and false otherwise.

exists

public boolean exists()
Returns true if the file exists and false otherwise.

getLength

public int getLength()
Returns the length of the file in bytes. If the file is not open 0 will be returned.

getPath

public java.lang.String getPath()
Return the file's path.

isDir

public boolean isDir()
Returns true if the file is a directory and false otherwise.

listDir

public java.lang.String[] listDir()
Lists the files contained in a directory. The strings returned are the names of the files and directories contained within this directory. This method returns null if the directory can't be read or if the operation fails.

readBytes

public int readBytes(byte[] b,
                     int off,
                     int len)
Reads bytes from the file into a byte array. Returns the number of bytes actually read or -1 if an error prevented the read operation from occurring. After the read is complete, the location of the file pointer (where read and write operations start from) is advanced the number of bytes read.
Overrides:
readBytes in class Stream
Parameters:
buf - the byte array to read data into
start - the start position in the array
count - the number of bytes to read

writeBytes

public int writeBytes(byte[] b,
                      int off,
                      int len)
Writes to the file. Returns the number of bytes written or -1 if an error prevented the write operation from occurring. After the write is complete, the file pointer (where read and write operations start from) is advanced the number of bytes written.
Overrides:
writeBytes in class Stream
Parameters:
buf - the byte array to write data from
start - the start position in the byte array
count - the number of bytes to write

rename

public boolean rename(java.lang.String path)
Renames the file. If the path given is in a different directory, the existing file will be moved to the directory in the specified path. Returns true if the renaming was successful and false otherwise.

seek

public boolean seek(int pos)
Sets the file pointer for read and write operations to the given position. The position passed is an absolute position, in bytes, from the beginning of the file. To set the position to just after the end of the file, you can call:
 file.seek(file.getLength());
 
True is returned if the operation is successful and false otherwise.