public class File extends RandomAccessStream implements FileStates
Note that writing to a storage card can be 5x slower than writing to the main memory.
Here is an example showing data being read from a file:
File file = new File("/temp/tempfile", File.READ_WRITE); byte b[] = new byte[10]; file.readBytes(b, 0, 10); file.close(); file = new File("/temp/tempfile"); // opens in DONT_OPEN mode file.delete();When creating a new file, you may start the path using the alias "device/", which evaluates to the platform's base user directory:
Modifier and Type | Field and Description |
---|---|
static int |
ATTR_ARCHIVE
Used in the getAttributes and setAttributes method.
|
static int |
ATTR_HIDDEN
Used in the getAttributes and setAttributes method.
|
static int |
ATTR_READ_ONLY
Used in the getAttributes and setAttributes method.
|
static int |
ATTR_SYSTEM
Used in the getAttributes and setAttributes method.
|
protected java.lang.String |
path
The path that represents this file
|
static byte |
TIME_ACCESSED
Used in the setTime method.
|
static byte |
TIME_ALL
Used in the setTime method, in parameter whichTime.
|
static byte |
TIME_CREATED
Used in the setTime method.
|
static byte |
TIME_MODIFIED
Used in the setTime method.
|
static java.lang.String[] |
winceVols
These are the volumes that getCardVolume search to find the available one.
|
pos, SEEK_CUR, SEEK_END, SEEK_SET
skipBuffer
CLOSED, CREATE, CREATE_EMPTY, DONT_OPEN, INVALID, READ_ONLY, READ_WRITE
Constructor and Description |
---|
File(java.lang.String path)
Opens a file with the given path and mode=DONT_OPEN and slot=-1.
|
File(java.lang.String path,
int mode)
Creates a file with the given path, mode and slot=-1.
|
File(java.lang.String path,
int mode,
int slot)
Deprecated.
TotalCross 2 no longer uses slot
|
Modifier and Type | Method and Description |
---|---|
int |
chmod(int mod)
Applies the given permissions to this file.
|
void |
close()
Closes the file.
|
static void |
copy(java.lang.String src,
java.lang.String dst)
A handy method to call copyTo creating two File instances and closing them.
|
void |
copyTo(File dest)
Copies the current file to the given one.
|
void |
createDir()
Recursively creates a directory that is represented by the current file.
|
void |
delete()
Deletes the file or directory (which must be empty).
|
static void |
deleteDir(java.lang.String dir)
Deletes a directory and all its subdirectories and files.
|
boolean |
exists()
Returns true if the file exists and false otherwise.
Example: |
protected void |
finalize() |
void |
flush()
Flushes a file.
|
int |
getAttributes()
Gets this file attributes.
|
static java.lang.String |
getCardSerialNumber(int slot)
Returns the card serial number for the given slot.
|
static File |
getCardVolume()
Returns the volume File for the Windows CE and Pocket PC, and BlackBerry devices.
|
File |
getParent()
Returns the file's parent, or null if its the root.
|
java.lang.String |
getPath()
Return the file's path passed in the constructor.
|
int |
getPos()
Returns the current offset in this stream.
|
int |
getSize()
Returns the size of the file in bytes.
|
int |
getSlot()
Returns the slot number passed in the constructor, or -1 if no slot was given.
|
Time |
getTime(byte whichTime)
Retrieves the specified time attribute of the opened file - cannot be used with
DONT_OPEN .JDK - If the object state and the received argument are valid, it will always return the time of the last modification. |
static boolean |
isCardInserted(int slot)
Can be used to verify if a card is inserted into the given slot.
|
boolean |
isDir()
Returns true if the file is a directory and false otherwise.
|
boolean |
isEmpty()
If this is a file, returns true if the file has 0 bytes.
|
java.lang.String[] |
listFiles()
Lists the files contained in a directory.
|
static java.lang.String[] |
listFiles(java.lang.String dir)
Returns a recursive list of all files inside the given directory (including it).
|
static java.lang.String[] |
listFiles(java.lang.String dir,
boolean recursive)
Lists all the files in the specified directory, and also the files in the subdirectories if recursive is true.
|
static java.lang.String[] |
listRoots()
List the root drives.
|
static void |
move(java.lang.String src,
java.lang.String dst)
A handy method to call moveTo creating two File instances and closing them.
|
void |
moveTo(File dest)
Moves the current file to the given one (the original file is deleted).
|
byte[] |
read()
Deprecated.
|
byte[] |
readAndClose()
Deprecated.
|
byte[] |
readAndDelete()
Deprecated.
|
int |
readBytes(byte[] b,
int off,
int len)
Reads bytes from the stream.
|
void |
rename(java.lang.String path)
Renames the file.
|
void |
setAttributes(int attr)
Sets the attributes of the opened file - cannot be used with
DONT_OPEN .Platform specific notes: JDK - This method has no effect on any file when running on JDK, it only checks if the object state and the received argument are valid. |
void |
setPos(int pos)
Sets the file pointer for read and write operations to the given position.
|
void |
setPos(int offset,
int origin)
Sets the file pointer for read and write operations to a new position defined by adding offset to a reference
position specified by origin.
|
void |
setSize(int newSize)
Sets the file size, growing the size or truncating it.
|
void |
setTime(byte whichTime,
Time time)
Sets the time attribute of the opened filed - cannot be used with
DONT_OPEN or READ_ONLY .Platform specific notes: JDK - This method has no effect on any file when running on JDK, it only checks if the object state and the received arguments are valid. |
void |
writeAndClose(byte[] bytes)
Deprecated.
|
int |
writeBytes(byte[] b,
int off,
int len)
Writes bytes to the stream.
|
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
protected java.lang.String path
public static final byte TIME_ALL
public static final byte TIME_CREATED
public static final byte TIME_MODIFIED
public static final byte TIME_ACCESSED
public static final int ATTR_ARCHIVE
setAttributes(int)
,
getAttributes()
,
Constant Field Valuespublic static final int ATTR_HIDDEN
setAttributes(int)
,
getAttributes()
,
Constant Field Valuespublic static final int ATTR_READ_ONLY
setAttributes(int)
,
getAttributes()
,
Constant Field Valuespublic static final int ATTR_SYSTEM
setAttributes(int)
,
getAttributes()
,
Constant Field Valuespublic static java.lang.String[] winceVols
/sdcard
. Be sure that the sdcard is NOT MOUNTED, otherwise your application will not have access to it.getCardVolume()
@Deprecated public File(java.lang.String path, int mode, int slot) throws IllegalArgumentIOException, FileNotFoundException, IOException
Note that it's not advised to use accentuated characters in the file name. Also, the slash / MUST be the path separator. It is not forbidden to use the backslash \, but its support might be discontinued in the future to increase performance. Note also that some OSes may not allow the creation of files in the ROOT directory.
path
- the file's path. Always use slashes (/) instead of backslashes (\\).
/sdcard
. Be sure that the sdcard is NOT MOUNTED, otherwise your application will not have access to it.mode
- one of open modes.slot
- The card slot number. This currently works only on Palm OS devices, because other OSes use a different
approach to specify the card. The number may be -1 to use the last available card, or a number between 0
and the number of cards supported by the device. Usually, slot 0 is the main memory (CAUTION: cannot be
used with File!), slot 1 is the NVFS volume, slot 2 the external card volume. This may vary on some Palm
devices, so use prefer using the Settings.nvfsVolume property. You can find the available slots using
this code:
for (int i = 0; i < 10; i++) if (File.isCardInserted(i)) add(new Label("found " + i), LEFT, AFTER); // Zire 22 returns 1 only
IllegalArgumentIOException
FileNotFoundException
IOException
File(String)
,
File(String,int)
,
FileStates.DONT_OPEN
,
FileStates.READ_WRITE
,
FileStates.READ_ONLY
,
FileStates.CREATE
,
FileStates.CREATE_EMPTY
,
Settings.nvfsVolume
public File(java.lang.String path, int mode) throws IllegalArgumentIOException, FileNotFoundException, IOException
path
- the file's pathmode
- one of open modesIllegalArgumentIOException
FileNotFoundException
IOException
File(String)
,
File(String,int,int)
public File(java.lang.String path) throws IllegalArgumentIOException, IOException
path
- the file's pathIllegalArgumentIOException
FileNotFoundException
IOException
File(String, int, int)
public static final boolean isCardInserted(int slot) throws IllegalArgumentIOException
slot
- The slot number, or -1 to use the last slot number (which, in most devices, will be the only slot
available).IllegalArgumentIOException
public void close() throws IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
IOException
- If a file is closed more than once,public void flush() throws IOException
IOException
public final void createDir() throws IOException
IOException
- If the file was closed, or if it was open in anything else than DONT_OPEN, or if the directory already
exists, or if the directories could not be created.
Example:
new File("/my/new/recursive/folder").createDir();
public final void delete() throws FileNotFoundException, IOException
new File("/my/file.c").delete();
FileNotFoundException
IOException
public final boolean exists() throws IOException
if (new File("dummy.txt").exists()) ...
IOException
public final int getSize() throws IOException
int freeSpace; if (Settings.platform.equals("Win32") || Settings.platform.equals("Java")) freeSpace = new File("c:\\").getSize(); else if (Settings.platform.equals("PalmOS")) freeSpace = new File("\\", 1).getSize(); // hidden volume else freeSpace = new File("\\").getSize(); // WinCE and Posix
IOException
public java.lang.String getPath()
public File getParent() throws IOException
IOException
public final boolean isDir() throws IOException
IOException
public final java.lang.String[] listFiles() throws IOException
IOException
public final boolean isEmpty() throws IOException
IOException
public final int readBytes(byte[] b, int off, int len) throws IOException
Stream
readBytes
in class Stream
b
- the byte array to read data intooff
- the start position in the arraylen
- the number of bytes to readIOException
public final void rename(java.lang.String path) throws IllegalArgumentIOException, IOException
path
- the new name of the file.IllegalArgumentIOException
IOException
public int getPos() throws IOException
RandomAccessStream
getPos
in class RandomAccessStream
IOException
- if an I/O error has occurred.public final void setPos(int pos) throws IOException
file.setPos(file.getSize());Note: if you plan to change the file size using setPos, you must write something on the new size to effectively change the size. For example, on some devices if you call setPos and then read (assuming that the new pos is past the end of the file, the read method will fail. Here's a code that will change the size for sure:
private static byte[] zeros = new byte[4096]; public void setSize(int newSize) { int size = f.getSize(); f.setPos(newSize - 1); // note: setPos(1) makes the file 2 bytes long (0, 1) f.setPos(size); for (int dif = newSize - size, n = 0; dif > 0; dif -= n) n = f.writeBytes(zeros, 0, dif > zeros.length ? zeros.length : dif); }
setPos
in class RandomAccessStream
pos
- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.IOException
- if pos is negative or if an I/O error occurs.public final void setPos(int offset, int origin) throws IOException
RandomAccessStream
setPos
in class RandomAccessStream
offset
- number of bytes to offset from origin.origin
- position from where offset is added. It is specified by one of the SEEK_* constants.IOException
- if the new position is negative or if an I/O error occurs.public final int writeBytes(byte[] b, int off, int len) throws IOException
Stream
IOException
if an error prevented the write
operation from occurring.writeBytes
in class Stream
b
- the byte array to write data fromoff
- the start position in the byte arraylen
- the number of bytes to writeIOException
public final void setAttributes(int attr) throws IllegalArgumentIOException, IOException
DONT_OPEN
.ATTR_HIDDEN
and ATTR_READ_ONLY
.
ATTR_HIDDEN
and ATTR_ARCHIVE
are not
supported by Unix based systems. Using them will not throw an exception, but it will have no effect on the file.
ATTR_READ_ONLY
on files located in the device's internal
storage. Marking a file as read only affects also its attributes, which means it can't be undone. The only way to
remove a file marked as read only is performing a hard reset.
attr
- one ore more ATTR_xxx constants ORed together.IllegalArgumentIOException
IOException
ATTR_ARCHIVE
,
ATTR_HIDDEN
,
ATTR_READ_ONLY
,
ATTR_SYSTEM
public final int getAttributes() throws IOException
This method does not work on desktop, but the arguments are still checked.
IOException
ATTR_ARCHIVE
,
ATTR_HIDDEN
,
ATTR_READ_ONLY
,
ATTR_SYSTEM
public final void setTime(byte whichTime, Time time) throws IllegalArgumentIOException, IOException
DONT_OPEN
or READ_ONLY
.TIME_MODIFIED
if the file is stored on the device's non-volatile memory. If the file is stored in an
external FAT storage, it also supports TIME_CREATED
.
whichTime
- One or more of the TIME_xxx constants, ORed together.time
- The new time.IllegalArgumentIOException
IOException
TIME_ALL
,
TIME_ACCESSED
,
TIME_CREATED
,
TIME_MODIFIED
public final Time getTime(byte whichTime) throws IllegalArgumentIOException, IOException
DONT_OPEN
.TIME_MODIFIED
if the file is stored on the device's non-volatile memory. If the file is stored in an
external FAT storage, it also supports TIME_CREATED
.
TIME_MODIFIED
.
TIME_CREATED
will return the last time the file was changed, which is updated when changes
are made to the file's inode (owner, permissions, etc.), and also when the contents of the file are modified.TIME_MODIFIED
returns the last time the contents of the file were modified.whichTime
- value must be TIME_ACCESSED
, TIME_CREATED
or TIME_MODIFIED
. Any
other value will result in an exception.IllegalArgumentIOException
IOException
TIME_ACCESSED
,
TIME_CREATED
,
TIME_MODIFIED
public static final File getCardVolume() throws IOException
/sdcard
. Be sure that the sdcard is NOT MOUNTED, otherwise your application will not have access to it.
Some android devices have more than one sdcard, an internal and an external ones. On such devices, /sdcard is the internal one; to find the external path, you must get into the device
because there's no API to get it. For example, on Galaxy devices, it is /mnt/extSdCard.IOException
winceVols
public final void setSize(int newSize) throws IOException
newSize
- The new file size.IOException
public static final java.lang.String getCardSerialNumber(int slot) throws IllegalArgumentIOException, IOException
This method only works on Palm OS.
slot
- The slot number, or -1 to use the last slot (which is usually an external card if the device has such
slot).IllegalArgumentIOException
IOException
protected void finalize()
finalize
in class java.lang.Object
public int getSlot()
public static java.lang.String[] listFiles(java.lang.String dir) throws IOException
IOException
public static java.lang.String[] listFiles(java.lang.String dir, boolean recursive) throws IOException
dir
- recursive
- IOException
public static void deleteDir(java.lang.String dir) throws IOException
Vm.gc()
after calling this method.IOException
public static java.lang.String[] listRoots()
public void copyTo(File dest) throws IOException
File src = new File(srcFileName,File.READ_WRITE); File dest = new File(destFileName,File.CREATE_EMPTY); src.copyTo(dest); src.close(); dest.close();This method is thread-safe.
IOException
moveTo(File)
public void moveTo(File dest) throws IOException
File src = new File(srcFileName,File.READ_WRITE); File dest = new File(destFileName,File.CREATE_EMPTY); src.moveTo(dest); // src.close(); - not needed! src was deleted dest.close();This method is thread-safe.
IOException
copyTo(File)
public static void copy(java.lang.String src, java.lang.String dst) throws IOException
IOException
copyTo(File)
public static void move(java.lang.String src, java.lang.String dst) throws IOException
IOException
moveTo(File)
public int chmod(int mod) throws IOException
Number Permission 000 --------- 400 r-------- 444 r--r--r-- 600 rw------- 620 rw--w---- 640 rw-r----- 644 rw-r--r-- 645 rw-r--r-x 646 rw-r--rw- 650 rw-r-x--- 660 rw-rw---- 661 rw-rw---x 662 rw-rw--w- 663 rw-rw--wx 664 rw-rw-r-- 666 rw-rw-r-- 700 rwx------ 750 rwxr-x--- 755 rwxr-xr-x 777 rwxrwxrwxThe numbers represents a group of 3. The first number is the permission for user, the second number for group, and the third number for others These are the possible permission values for each number:
Permission Binary Decimal --- 000 0 --x 001 1 -w- 010 2 -wx 011 3 r-- 100 4 r-x 101 5 rw- 110 6 rwx 111 7Failing to change the permission returns -1.
try { // testing in a folder File f = new File(Settings.appPath); add(new Label("mods of appPath = "+f.chmod(-1)),CENTER,CENTER); // testing in a file String name = "test"; f = new File(Settings.appPath+'/'+name,File.CREATE_EMPTY); int m0 = f.chmod(777); // change it int m1 = f.chmod(-1); // retrieve the changed value add(new Label("mods of "+name+" = "+m0+" -> "+m1+" (777)"),CENTER,AFTER+5); } catch (Exception ee) { MessageBox.showException(ee,true); }
mod
- The modifiers you want to set in DECIMAL, or -1 to just return the current ones.IOException
@Deprecated public byte[] readAndClose() throws IOException
byte[] bytes = new File(...,File.READ_ONLY).readAndClose();The only drawback is that this method consumes lots of memory if the file is big; use it carefully.
IOException
@Deprecated public byte[] readAndDelete() throws IOException
byte[] bytes = new File(...,File.READ_ONLY).readAndDelete();The only drawback is that this method consumes lots of memory if the file is big; use it carefully.
IOException
@Deprecated public void writeAndClose(byte[] bytes) throws IOException
new File(...,File.CREATE_EMPTY).writeAndClose(Vm.getFile("myfile.txt"));The only drawback is that this method consumes lots of memory if the file is big; use it carefully.
IOException
@Deprecated public byte[] read() throws IOException
IOException