Package totalcross.io
Class File
- java.lang.Object
-
- totalcross.io.Stream
-
- totalcross.io.RandomAccessStream
-
- totalcross.io.File
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,FileStates
public class File extends RandomAccessStream implements FileStates
File represents a file or directory.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:- PalmOS, WinCE and iPhone - "/" (root)
- BlackBerry - "/store/home/user/"
- Java - "/" (current directory)
- Win32 - "/" (root of the current drive)
- iOS - "/private/var/" in a installation using .deb (on a jailbroken device) or in the documents folder of the application in a installation using .ipa
- Android - "/data/data/totalcross.app.<mainclass name>" or "/data/data/totalcross.app.<application id> if using single package
On iOS and Android, if you don't specify a path, the file will be open in device/ path.
-
-
Field Summary
Fields Modifier and Type Field Description static intATTR_ARCHIVEUsed in the getAttributes and setAttributes method.static intATTR_HIDDENUsed in the getAttributes and setAttributes method.static intATTR_READ_ONLYUsed in the getAttributes and setAttributes method.static intATTR_SYSTEMUsed in the getAttributes and setAttributes method.static java.lang.StringDIRECTORY_DOCUMENTSstatic java.lang.StringDIRECTORY_DOWNLOADSstatic java.lang.StringDIRECTORY_PICTURESprotected java.lang.StringpathThe path that represents this filestatic byteTIME_ACCESSEDUsed in the setTime method.static byteTIME_ALLUsed in the setTime method, in parameter whichTime.static byteTIME_CREATEDUsed in the setTime method.static byteTIME_MODIFIEDUsed in the setTime method.static java.lang.String[]winceVolsThese are the volumes that getCardVolume search to find the available one.-
Fields inherited from class totalcross.io.RandomAccessStream
pos, SEEK_CUR, SEEK_END, SEEK_SET
-
Fields inherited from class totalcross.io.Stream
skipBuffer
-
Fields inherited from interface totalcross.io.FileStates
CLOSED, CREATE, CREATE_EMPTY, DONT_OPEN, INVALID, READ_ONLY, READ_WRITE
-
-
Constructor Summary
Constructors Constructor 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
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description intchmod(int mod)Applies the given permissions to this file.voidclose()Closes the file.static voidcopy(java.lang.String src, java.lang.String dst)A handy method to call copyTo creating two File instances and closing them.voidcopyTo(File dest)Copies the current file to the given one.voidcreateDir()Recursively creates a directory that is represented by the current file.voiddelete()Deletes the file or directory (which must be empty).static voiddeleteDir(java.lang.String dir)Deletes a directory and all its subdirectories and files.booleanexists()Returns true if the file exists and false otherwise.
Example:protected voidfinalize()voidflush()Flushes a file.static FilegetAppSpecificDir(java.lang.String type, java.lang.String dirName)Returns a file object with a path writable by the application on the device storage.intgetAttributes()Gets this file attributes.static java.lang.StringgetCardSerialNumber(int slot)Returns the card serial number for the given slot.static FilegetCardVolume()Returns the volume File for the Windows CE and Pocket PC, and BlackBerry devices.FilegetParent()Returns the file's parent, or null if its the root.java.lang.StringgetPath()Return the file's path passed in the constructor.intgetPos()Returns the current offset in this stream.intgetSize()Returns the size of the file in bytes.intgetSlot()Returns the slot number passed in the constructor, or -1 if no slot was given.TimegetTime(byte whichTime)Retrieves the specified time attribute of the opened file - cannot be used withDONT_OPEN.
JDK - If the object state and the received argument are valid, it will always return the time of the last modification.static booleanisCardInserted(int slot)Can be used to verify if a card is inserted into the given slot.booleanisDir()Returns true if the file is a directory and false otherwise.booleanisEmpty()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 voidmove(java.lang.String src, java.lang.String dst)A handy method to call moveTo creating two File instances and closing them.voidmoveTo(File dest)Moves the current file to the given one (the original file is deleted).byte[]read()Deprecated.byte[]readAndClose()Deprecated.byte[]readAndDelete()Deprecated.intreadBytes(byte[] b, int off, int len)Reads bytes from the stream.voidrename(java.lang.String path)Renames the file.voidsetAttributes(int attr)Sets the attributes of the opened file - cannot be used withDONT_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.voidsetPos(int pos)Sets the file pointer for read and write operations to the given position.voidsetPos(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.voidsetSize(int newSize)Sets the file size, growing the size or truncating it.voidsetTime(byte whichTime, Time time)Sets the time attribute of the opened filed - cannot be used withDONT_OPENorREAD_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.voidwriteAndClose(byte[] bytes)Deprecated.intwriteBytes(byte[] b, int off, int len)Writes bytes to the stream.-
Methods inherited from class totalcross.io.Stream
asInputStream, asOutputStream, asStream, asStream, skipBytes, wrapInputStream, wrapInputStreamToStream, write, writeBytes, writeBytes, writeBytes
-
-
-
-
Field Detail
-
path
protected java.lang.String path
The path that represents this file
-
TIME_ALL
public static final byte TIME_ALL
Used in the setTime method, in parameter whichTime. This sets all times at once.
-
TIME_CREATED
public static final byte TIME_CREATED
Used in the setTime method. These values are platform independent, and can be ORed together in the setTime method.
-
TIME_MODIFIED
public static final byte TIME_MODIFIED
Used in the setTime method. These values are platform independent, and can be ORed together in the setTime method.
-
TIME_ACCESSED
public static final byte TIME_ACCESSED
Used in the setTime method. These values are platform independent, and can be ORed together in the setTime method.
-
ATTR_ARCHIVE
public static final int ATTR_ARCHIVE
Used in the getAttributes and setAttributes method. These values are platform independent.- See Also:
setAttributes(int),getAttributes(), Constant Field Values
-
ATTR_HIDDEN
public static final int ATTR_HIDDEN
Used in the getAttributes and setAttributes method. These values are platform independent.- See Also:
setAttributes(int),getAttributes(), Constant Field Values
-
ATTR_READ_ONLY
public static final int ATTR_READ_ONLY
Used in the getAttributes and setAttributes method. These values are platform independent. Palm specific: Avoid using this attribute on a file located on the Built-in storage, because some devices do not allow this attribute to be changed after it is first set. This results in a read-only file that cannot be changed or deleted.- See Also:
setAttributes(int),getAttributes(), Constant Field Values
-
ATTR_SYSTEM
public static final int ATTR_SYSTEM
Used in the getAttributes and setAttributes method. These values are platform independent.- See Also:
setAttributes(int),getAttributes(), Constant Field Values
-
winceVols
public static java.lang.String[] winceVols
These are the volumes that getCardVolume search to find the available one.
To access the card in Android devices, prefix the path with/sdcard. Be sure that the sdcard is NOT MOUNTED, otherwise your application will not have access to it.- See Also:
getCardVolume()
-
DIRECTORY_PICTURES
public static final java.lang.String DIRECTORY_PICTURES
- See Also:
- Constant Field Values
-
DIRECTORY_DOCUMENTS
public static final java.lang.String DIRECTORY_DOCUMENTS
- See Also:
- Constant Field Values
-
DIRECTORY_DOWNLOADS
public static final java.lang.String DIRECTORY_DOWNLOADS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
File
@Deprecated public File(java.lang.String path, int mode, int slot) throws IllegalArgumentIOException, FileNotFoundException, IOExceptionDeprecated.TotalCross 2 no longer uses slotOpens a file with the given name, mode and in the given card number.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.
- Parameters:
path- the file's path. Always use slashes (/) instead of backslashes (\\).
To access the card in Android devices, prefix the path with/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- Throws:
IllegalArgumentIOExceptionFileNotFoundExceptionIOException- Since:
- SuperWaba 5.52
- See Also:
File(String),File(String,int),FileStates.DONT_OPEN,FileStates.READ_WRITE,FileStates.READ_ONLY,FileStates.CREATE,FileStates.CREATE_EMPTY,Settings.nvfsVolume
-
File
public File(java.lang.String path, int mode) throws IllegalArgumentIOException, FileNotFoundException, IOExceptionCreates a file with the given path, mode and slot=-1.- Parameters:
path- the file's pathmode- one of open modes- Throws:
IllegalArgumentIOExceptionFileNotFoundExceptionIOException- See Also:
File(String),File(String,int,int)
-
File
public File(java.lang.String path) throws IllegalArgumentIOException, IOExceptionOpens a file with the given path and mode=DONT_OPEN and slot=-1. This constructor is useful for directory manipulation and/or check if file exists. No read/write operation can be done with a file created in this mode.- Parameters:
path- the file's path- Throws:
IllegalArgumentIOExceptionFileNotFoundExceptionIOException- See Also:
File(String, int, int)
-
-
Method Detail
-
isCardInserted
public static final boolean isCardInserted(int slot) throws IllegalArgumentIOExceptionCan be used to verify if a card is inserted into the given slot. Only works on Palm OS and Android devices. In all other platforms, always returns true.- Parameters:
slot- The slot number, or -1 to use the last slot number (which, in most devices, will be the only slot available).- Throws:
IllegalArgumentIOException- Since:
- SuperWaba 5.52
-
close
public void close() throws IOExceptionCloses the file.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
IOException- If a file is closed more than once,
-
flush
public void flush() throws IOExceptionFlushes a file. This causes any pending data to be written to disk. Calling this method too much may decrease the performance. Has no effect on JavaSE.- Throws:
IOException
-
createDir
public final void createDir() throws IOExceptionRecursively creates a directory that is represented by the current file. The file must have been open in DONT_OPEN mode (remember that the constructor File(String) already opens the file in DONT_OPEN mode).- Throws:
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();
-
delete
public final void delete() throws FileNotFoundException, IOExceptionDeletes the file or directory (which must be empty). The file is automatically closed before it is deleted. The file could have been opened in any of the available modes, except READ_ONLY. Example:new File("/my/file.c").delete();- Throws:
FileNotFoundExceptionIOException
-
exists
public final boolean exists() throws IOExceptionReturns true if the file exists and false otherwise.
Example:if (new File("dummy.txt").exists()) ...- Throws:
IOException
-
getSize
public final int getSize() throws IOExceptionReturns the size of the file in bytes. If the file is a directory (ends with slash, not backslash), it returns the amount of free space in bytes of the card/file system (also works on JDK 1.6 and above).
If the total amount is greater than 2 GB, 2 GB is returned. In other cases, if the file is not opened, an exception will be thrown.
Examples: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- Throws:
IOException
-
getPath
public java.lang.String getPath()
Return the file's path passed in the constructor.
-
getParent
public File getParent() throws IOException
Returns the file's parent, or null if its the root.- Throws:
IOException
-
isDir
public final boolean isDir() throws IOExceptionReturns true if the file is a directory and false otherwise. The file must have been open in DONT_OPEN mode.- Throws:
IOException
-
listFiles
public final java.lang.String[] listFiles() throws IOExceptionLists the files contained in a directory. The strings returned are the names of the files and directories contained within this directory. Paths are suffixed by a slash.- Throws:
IOException
-
isEmpty
public final boolean isEmpty() throws IOExceptionIf this is a file, returns true if the file has 0 bytes. If this is a folder, returns true if there are no files nor folders inside of it.- Throws:
IOException- Since:
- TotalCross 1.27
-
readBytes
public final int readBytes(byte[] b, int off, int len) throws IOExceptionDescription copied from class:StreamReads bytes from the stream. Returns the number of bytes actually read or -1 if the end of the stream was reached. (if applicable to the stream)- Specified by:
readBytesin classStream- Parameters:
b- the byte array to read data intooff- the start position in the arraylen- the number of bytes to read- Throws:
IOException
-
rename
public final void rename(java.lang.String path) throws IllegalArgumentIOException, IOExceptionRenames the file. You must give the full directory specification for the file, to keep compatibility between all platforms. WinCE platform lets you move a file using rename, while Palm OS does not let you move the file. File is automatically closed prior to renaming. After this operation, this File object is invalid. Cannot be used in READ_ONLY mode.- Parameters:
path- the new name of the file.- Throws:
IllegalArgumentIOExceptionIOException
-
getPos
public int getPos() throws IOExceptionDescription copied from class:RandomAccessStreamReturns the current offset in this stream.- Overrides:
getPosin classRandomAccessStream- Returns:
- the offset from the beginning of the stream, in bytes, at which the next read or write occurs.
- Throws:
IOException- if an I/O error has occurred.
-
setPos
public final void setPos(int pos) throws IOExceptionSets 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.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); }- Specified by:
setPosin classRandomAccessStream- Parameters:
pos- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.- Throws:
IOException- if pos is negative or if an I/O error occurs.
-
setPos
public final void setPos(int offset, int origin) throws IOExceptionDescription copied from class:RandomAccessStreamSets the file pointer for read and write operations to a new position defined by adding offset to a reference position specified by origin.- Specified by:
setPosin classRandomAccessStream- Parameters:
offset- number of bytes to offset from origin.origin- position from where offset is added. It is specified by one of the SEEK_* constants.- Throws:
IOException- if the new position is negative or if an I/O error occurs.
-
writeBytes
public final int writeBytes(byte[] b, int off, int len) throws IOExceptionDescription copied from class:StreamWrites bytes to the stream. Returns the number of bytes actually written or throws anIOExceptionif an error prevented the write operation from occurring.- Specified by:
writeBytesin classStream- Parameters:
b- the byte array to write data fromoff- the start position in the byte arraylen- the number of bytes to write- Returns:
- the number of bytes actually written
- Throws:
IOException
-
setAttributes
public final void setAttributes(int attr) throws IllegalArgumentIOException, IOExceptionSets the attributes of the opened file - cannot be used withDONT_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.
- BLACKBERRY - Supports only
ATTR_HIDDENandATTR_READ_ONLY. - LINUX, IPHONE, and ANDROID - The attributes
ATTR_HIDDENandATTR_ARCHIVEare not supported by Unix based systems. Using them will not throw an exception, but it will have no effect on the file. - PALMOS - Avoid using the attribute
ATTR_READ_ONLYon 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.
- Parameters:
attr- one ore more ATTR_xxx constants ORed together.- Throws:
IllegalArgumentIOExceptionIOException- See Also:
ATTR_ARCHIVE,ATTR_HIDDEN,ATTR_READ_ONLY,ATTR_SYSTEM
-
getAttributes
public final int getAttributes() throws IOExceptionGets this file attributes. The file must be opened in a mode different of DONT_OPEN.This method does not work on desktop, but the arguments are still checked.
- Returns:
- The file attributes ORed together.
- Throws:
IOException- See Also:
ATTR_ARCHIVE,ATTR_HIDDEN,ATTR_READ_ONLY,ATTR_SYSTEM
-
setTime
public final void setTime(byte whichTime, Time time) throws IllegalArgumentIOException, IOExceptionSets the time attribute of the opened filed - cannot be used withDONT_OPENorREAD_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.
- WINCE - Supports only
TIME_MODIFIEDif the file is stored on the device's non-volatile memory. If the file is stored in an external FAT storage, it also supportsTIME_CREATED. - BLACKBERRY - Not supported.
- LINUX, IPHONE and ANDROID - Unix based systems do not keep record of the file's creation time. Attempting to do so will not thrown an exception, but it will have no effect on the file.
- Parameters:
whichTime- One or more of the TIME_xxx constants, ORed together.time- The new time.- Throws:
IllegalArgumentIOExceptionIOException- See Also:
TIME_ALL,TIME_ACCESSED,TIME_CREATED,TIME_MODIFIED
-
getTime
public final Time getTime(byte whichTime) throws IllegalArgumentIOException, IOException
Retrieves the specified time attribute of the opened file - cannot be used withDONT_OPEN.
- JDK - If the object state and the received argument are valid, it will always return the time of the last modification.
- WINCE - Supports only
TIME_MODIFIEDif the file is stored on the device's non-volatile memory. If the file is stored in an external FAT storage, it also supportsTIME_CREATED. - BLACKBERRY - Supports only
TIME_MODIFIED. - LINUX, IPHONE and ANDROID - Unix based systems do not keep record of the file's creation time. Using the
constant
TIME_CREATEDwill 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.
The constantTIME_MODIFIEDreturns the last time the contents of the file were modified.
- Parameters:
whichTime- value must beTIME_ACCESSED,TIME_CREATEDorTIME_MODIFIED. Any other value will result in an exception.- Throws:
IllegalArgumentIOExceptionIOException- See Also:
TIME_ACCESSED,TIME_CREATED,TIME_MODIFIED
-
getCardVolume
public static final File getCardVolume() throws IOException
Returns the volume File for the Windows CE and Pocket PC, and BlackBerry devices. On these devices, the volume has a special folder name, but since there's no system call that informs this, we must just test the existence of each folder, returning the first one that exists. You can set the winceVols string array to the ones you want to be searched.
To access the card on Android devices, prefix the path with/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.- Returns:
- The File object which references the volume, ended with backslash, or null if none found.
- Throws:
IOException- See Also:
winceVols
-
setSize
public final void setSize(int newSize) throws IOExceptionSets the file size, growing the size or truncating it.- Parameters:
newSize- The new file size.- Throws:
IOException- Since:
- SuperWaba 5.83
-
getCardSerialNumber
public static final java.lang.String getCardSerialNumber(int slot) throws IllegalArgumentIOException, IOExceptionReturns the card serial number for the given slot.This method only works on Palm OS.
- Parameters:
slot- The slot number, or -1 to use the last slot (which is usually an external card if the device has such slot).- Throws:
IllegalArgumentIOExceptionIOException- Since:
- TotalCross 1.0
-
finalize
protected void finalize()
- Overrides:
finalizein classjava.lang.Object
-
getSlot
public int getSlot()
Returns the slot number passed in the constructor, or -1 if no slot was given.- Since:
- TotalCross 1.0
-
listFiles
public static java.lang.String[] listFiles(java.lang.String dir) throws IOExceptionReturns a recursive list of all files inside the given directory (including it). The array is sorted upon return.- Throws:
IOException- Since:
- TotalCross 1.15
-
listFiles
public static java.lang.String[] listFiles(java.lang.String dir, boolean recursive) throws IOExceptionLists all the files in the specified directory, and also the files in the subdirectories if recursive is true.- Parameters:
dir-recursive-- Throws:
IOException
-
deleteDir
public static void deleteDir(java.lang.String dir) throws IOExceptionDeletes a directory and all its subdirectories and files. If you have problems trying to recreate the directory, be sure to callVm.gc()after calling this method.- Throws:
IOException- Since:
- TotalCross 1.15
-
listRoots
public static java.lang.String[] listRoots()
List the root drives. If there are no roots, returns null. Works on Win32, Java, and Blackberry platforms.- Since:
- TotalCross 1.22
-
copyTo
public void copyTo(File dest) throws IOException
Copies the current file to the given one. You must close both files after calling this method. Here's a sample of how to copy a file: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.- Throws:
IOException- Since:
- TotalCross 1.27
- See Also:
moveTo(File)
-
moveTo
public void moveTo(File dest) throws IOException
Moves the current file to the given one (the original file is deleted). You must explicitly close the destination file after this operation is done. Here's a sample of how to move a file: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.- Throws:
IOException- Since:
- TotalCross 1.27
- See Also:
copyTo(File)
-
copy
public static void copy(java.lang.String src, java.lang.String dst) throws IOExceptionA handy method to call copyTo creating two File instances and closing them. The target file is erased if it exists. This method is thread-safe. If you want to have more control, use the copyTo method- Throws:
IOException- Since:
- TotalCross 1.27
- See Also:
copyTo(File)
-
move
public static void move(java.lang.String src, java.lang.String dst) throws IOExceptionA handy method to call moveTo creating two File instances and closing them. The target file is erased if it exists. This method is thread-safe. If you want to have more control, use the other moveTo method- Throws:
IOException- Since:
- TotalCross 1.27
- See Also:
moveTo(File)
-
chmod
public int chmod(int mod) throws IOExceptionApplies the given permissions to this file. Works only on Unix-based operating systems: Linux, Android, and iOS. On JDK 1.6, the first number (user) is applied to all groups. Below you see a table with some chmod values (r = read, w = write, x = execute).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.
Here's a sample: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); }- Parameters:
mod- The modifiers you want to set in DECIMAL, or -1 to just return the current ones.- Returns:
- The modifiers that were set before you called this method (or the current modifiers, if -1 is being passed). Some platforms may return more than 3 digits, indicating extra attributes (for example, if it's a file or a directory).
- Throws:
IOException- Since:
- TotalCross 1.27
-
readAndClose
@Deprecated public byte[] readAndClose() throws IOExceptionDeprecated.Reads the entire file into a byte array and closes itself. A handy method that can be used like this: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.- Throws:
IOException- Since:
- TotalCross 1.53
-
readAndDelete
@Deprecated public byte[] readAndDelete() throws IOExceptionDeprecated.Reads the entire file into a byte array and DELETES itself. A handy method that can be used like this: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.- Throws:
IOException- Since:
- TotalCross 1.53
-
writeAndClose
@Deprecated public void writeAndClose(byte[] bytes) throws IOExceptionDeprecated.Writes byte array to this file and closes itself. A handy method that can be used like this: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.- Throws:
IOException- Since:
- TotalCross 1.53
-
read
@Deprecated public byte[] read() throws IOExceptionDeprecated.Reads the file and returns a byte array with its contents.- Throws:
IOException- Since:
- TotalCross 3.1
-
getAppSpecificDir
public static File getAppSpecificDir(java.lang.String type, java.lang.String dirName) throws IOException
Returns a file object with a path writable by the application on the device storage.- Parameters:
type-dirName-- Returns:
- Throws:
IOException
-
-