Class FileSystemStorage
Unlike networking, the file system storage mostly tries to emulate java.io.File with some simplifications for mobile devices.
Check out a more thorough discussion of this API here.
A lot of API's rely on FileSystemStorage as its the API native code usually uses consistently.
E.g. in this sample below the FileSystemStorage is used to save a screenshot image for sharing
on social media:
Form hi = new Form("ShareButton");
ShareButton sb = new ShareButton();
sb.setText("Share Screenshot");
hi.add(sb);
Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight());
hi.revalidate();
hi.setVisible(true);
hi.paintComponent(screenshot.getGraphics(), true);
String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png";
try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) {
ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1);
} catch(IOException err) {
Log.e(err);
}
sb.setImageToShare(imageFile, "image/png");
The sample below shows the FileSystemStorage as a tree:
Form hi = new Form("FileSystemTree", new BorderLayout());
TreeModel tm = new TreeModel() {
@Override
public Vector getChildren(Object parent) {
String[] files;
if(parent == null) {
files = FileSystemStorage.getInstance().getRoots();
return new Vector(Arrays.asList(files));
} else {
try {
files = FileSystemStorage.getInstance().listFiles((String)parent);
} catch(IOException err) {
Log.e(err);
files = new String[0];
}
}
String p = (String)parent;
Vector result = new Vector();
for(String s : files) {
result.add(p + s);
}
return result;
}
@Override
public boolean isLeaf(Object node) {
return !FileSystemStorage.getInstance().isDirectory((String)node);
}
};
Tree t = new Tree(tm) {
@Override
protected String childToDisplayLabel(Object child) {
String n = (String)child;
int pos = n.lastIndexOf("/");
if(pos
@author Shai Almog
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intRepresents the type for the get root type method, this type generally represents the main phone memorystatic final intRepresents the type for the get root type method, this type generally represents an SD card although due to variability in phone standards an SD card might be detected incorrectly.static final intReturned for different types of root for which there is no specific knowledge one way or the other. -
Method Summary
Modifier and TypeMethodDescriptionvoidDeletes the specific file or empty directory.voiddeleteRetry(String file, int retryCount) Deletes the specific file or empty directory, if the platform supports a delete on exit this method will activate it.booleanIndicates whether a file existsThe application home directory is a "safe place" to store files for this application in a portable way.Returns a device specific directory designed for cache style files, or null if#hasCachesDir()is falsecharReturns the file system separator char normally '/'static FileSystemStorageThis class is a singletonlonggetLastModified(String file) Returns the time that the file denoted by this abstract pathname was last modified.longReturns the length of the filelonggetRootAvailableSpace(String root) Returns the available space in the given root directoryString[]getRoots()Returns the filesystem roots from which the structure of the file system can be traversedlonggetRootSizeBytes(String root) Returns the size of the given root directoryintgetRootType(String root) Returns the type of the root often by guessingbooleanReturns true if the device has a directory dedicated for "cache" filesbooleanisDirectory(String file) Indicates whether the given file is a directorybooleanIndicates the hidden state of the fileString[]Lists the files within the given directory, returns relative file names and not full file names.voidCreates the given directoryopenInputStream(String file) Opens an input stream to the given fileopenOutputStream(String file) Opens an output stream to the given fileopenOutputStream(String file, int offset) Opens an output stream to the given filevoidRenames a file to the given name, expects the new name to be relative to the current directoryvoidToggles the hidden state of the filetoNativePath(String path) Converts a file system path to a native path.
-
Field Details
-
ROOT_TYPE_MAINSTORAGE
public static final int ROOT_TYPE_MAINSTORAGERepresents the type for the get root type method, this type generally represents the main phone memory- See Also:
-
ROOT_TYPE_SDCARD
public static final int ROOT_TYPE_SDCARDRepresents the type for the get root type method, this type generally represents an SD card although due to variability in phone standards an SD card might be detected incorrectly. E.g. newer Nokia devices such as N97 have a large storage area that is marked as "E:" but is really internal storage. If an SD card isn't physically in the phone the "F:" won't be returned and it will be impossible to detect that "E:" is not the actual SD card.- See Also:
-
ROOT_TYPE_UNKNOWN
public static final int ROOT_TYPE_UNKNOWNReturned for different types of root for which there is no specific knowledge one way or the other.- See Also:
-
-
Method Details
-
getInstance
This class is a singleton
Returns
instance of this class
-
getRoots
Returns the filesystem roots from which the structure of the file system can be traversed
Returns
the roots of the filesystem
-
getRootType
Returns the type of the root often by guessing
Parameters
root: the root whose type we are checking
Returns
one of the type constants above
-
listFiles
Lists the files within the given directory, returns relative file names and not full file names.
Parameters
directory: the directory in which files should be listed
Returns
array of file names
- Throws:
IOException
-
getRootSizeBytes
Returns the size of the given root directory
Parameters
root: the root directory in the filesystem
Returns
the byte size of the directory
-
getRootAvailableSpace
Returns the available space in the given root directory
Parameters
root: the root directory in the filesystem
Returns
the bytes available in the directory
-
mkdir
Creates the given directory
Parameters
directory: the directory name to create
-
delete
Deletes the specific file or empty directory.
Parameters
file: file or empty directory to delete
-
deleteRetry
Deletes the specific file or empty directory, if the platform supports a delete on exit this method will activate it. Regardless it will retry deleting (with delay) several times to allow streams time to close.
Parameters
-
file: file to delete -
retryCount: the number of times to retry
-
-
exists
Indicates whether a file exists
Parameters
file: the file to check
Returns
true if the file exists and false otherwise
-
isHidden
Indicates the hidden state of the file
Parameters
file: file
Returns
true for a hidden file
-
setHidden
Toggles the hidden state of the file
Parameters
-
file: file -
h: hidden state
-
-
rename
-
getLength
Returns the length of the file
Parameters
file: file
Returns
length of said file
-
getLastModified
Returns the time that the file denoted by this abstract pathname was last modified.
Returns
- Returns:
A long value representing the time the file was last modified, measured in milliseconds
Deprecated
-
isDirectory
Indicates whether the given file is a directory
Parameters
file: file
Returns
true if its a directory
-
getFileSystemSeparator
public char getFileSystemSeparator()Returns the file system separator char normally '/'
Returns
the separator char
-
openOutputStream
Opens an output stream to the given file
Parameters
file: the file
Returns
the output stream
- Throws:
IOException
-
openInputStream
Opens an input stream to the given file
Parameters
file: the file
Returns
the input stream
- Throws:
IOException
-
openOutputStream
Opens an output stream to the given file
Parameters
-
file: the file -
offset: position in the file
Returns
the output stream
- Throws:
IOException
-
-
getAppHomePath
The application home directory is a "safe place" to store files for this application in a portable way. On some platforms such as Android & iOS this path may be visible only to the application itself, other apps won't have permission to access this path.
The sample below uses the app home directory to save a file so we can share it using the
com.codename1.components.ShareButton:Form hi = new Form("ShareButton"); ShareButton sb = new ShareButton(); sb.setText("Share Screenshot"); hi.add(sb); Image screenshot = Image.createImage(hi.getWidth(), hi.getHeight()); hi.revalidate(); hi.setVisible(true); hi.paintComponent(screenshot.getGraphics(), true); String imageFile = FileSystemStorage.getInstance().getAppHomePath() + "screenshot.png"; try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(imageFile)) { ImageIO.getImageIO().save(screenshot, os, ImageIO.FORMAT_PNG, 1); } catch(IOException err) { Log.e(err); } sb.setImageToShare(imageFile, "image/png");Returns
a writable directory that represent the application home directory
-
hasCachesDir
public boolean hasCachesDir()Returns true if the device has a directory dedicated for "cache" files
Returns
true if a caches style directory exists in this device type
-
getCachesDir
Returns a device specific directory designed for cache style files, or null if
#hasCachesDir()is falseReturns
file URL or null
-
toNativePath
-