MicroEJ multisandbox firmware demos disable access to the file system in
the application context. To store/retrieve information you should use a
service provided by the firmware. This service is called Storage and
like any other services it can be retrieved by using the default
ServiceLoader
(if you want more information about Service, please
read the firmware documentation).
Storage storage =
ServiceLoaderFactory.getServiceLoader().getService(Storage.class);
This service is based on key/value principle. The value is Java’s
InputStream
and the keys are Java’s String
(with some
restrictions on the allowed characters, see the javadoc).
The store
method needs an input stream and a key. During the method
execution the given input stream is entirely read, closed and stored in
the underlying file system (can be volatile or not depend on your
firmware).
String key = "MY_DATA"; //$NON-NLS-1$ try (ByteArrayInputStream bais = new ByteArrayInputStream("mydata".getBytes())) { //$NON-NLS-1$ storage.store(key, bais); } catch (IOException e) { e.printStackTrace(); }
Call the load
method with the same key used to store the data, to
retrieve it.
try (InputStream stream = storage.load(key)) { // Do something with the input stream. } catch (IOException e) { e.printStackTrace(); }
The application must close the input stream when it is no longer needed.
An application can list all stored data by calling the getIds method. This method returns all data key’s already stored.
try { for (String k : storage.getIds()) { System.out.println("Data available " + k); } } catch (IOException e) { e.printStackTrace(); }
Call the remove
method with the data key’s to remove it.
try { storage.remove(key); } catch (IOException e) { e.printStackTrace(); }
- Right Click on the project
- Select Run as -> MicroEJ Application
- Select your virtual device.
- Press Ok
- Right Click on the example
- Select Run as -> Run Configuration
- Select MicroEJ Application configuration
- Click on New launch configuration icon
- In Execution tab
- In Target frame, in Platform field, select the relevant virtual device.
- In Execution frame
- Select Execute on Device
- In Settings field, select Local Deployment(…)
- In Configurations tab, set the options of the deployment
- Press Apply
- Press Run
This example has been tested on:
- MicroEJ SDK 5.1
- With a BLACK-ESP32WROVER-RQQAW board virtual device
All dependencies are retrieved transitively by Ivy resolver.
N/A
None.