Thales' cellular IoT products business is now part of Telit Cinterion, find out more.

You are here

Telit Cinterion IoT Developer Community

Example how to use the flash file system (FFS)

Tutorial, January 30, 2015 - 11:43am, 7570 views

Here are an example for to use the flash file system of the java enabled module. The example shows for to create DIRs and files, to read, write and delete.

The FileConnection is handled in the File.class (File..java), the methods are called from the FileExample class as listed below.

....and here the File.class where the streams and connection is handled.

 import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
 * Class to handle a file on the FFS
 * 
 * @author Markus Enck
 * 
 */
public class File {
	private boolean debug = false;
	private FileConnection fc = null;
	private OutputStream os = null;
	private InputStream is = null;
	private String fileName = "example.txt";
	private String pathName = "logs/";
	final String rootName = "file:///a:/";

	public File(String fileName, String pathName) {
		super();
		this.fileName = fileName;
		this.pathName = pathName;
		if (!pathName.endsWith("/")) {
			this.pathName += "/"; // add a slash
		}
	}

	/**
	 * Debug flag
	 * 
	 * @return true if debug is enabled
	 */

	public boolean isDebug() {
		return debug;
	}

	/**
	 * Debug flag
	 * 
	 * @param debug
	 *            true for some additional system out
	 */
	public void setDebug(boolean debug) {
		this.debug = debug;
	}

	/**
	 * Writes a String to the FFS
	 * 
	 * @param lastKnownPos
	 * @throws IOException
	 */
	public void write(String text) throws IOException {
		write(text.getBytes());
	}

	/**
	 * Writes a byte array to the FFS
	 * 
	 * @param bytes
	 * @throws IOException
	 */
	public void write(byte[] bytes) throws IOException {
		if (debug)
			System.out.println(new String(bytes));
		os.write(bytes);
	}

	/**
	 * open and create log file
	 * 
	 * @throws IOException
	 */

	private FileConnection getFileConnection() throws IOException {
		// check if subfolder exists
		fc = (FileConnection) Connector.open(rootName + pathName);
		if (!fc.exists() || !fc.isDirectory()) {
			fc.mkdir();
			if (debug)
				System.out.println("Dir created");
		}
		// open file
		fc = (FileConnection) Connector.open(rootName + pathName + fileName);
		if (!fc.exists())
			fc.create();
		return fc;
	}

	/**
	 * release resources
	 */
	public void close() {
		if (is != null)
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		is = null;
		if (os != null)
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		os = null;
		if (fc != null)
			try {
				fc.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		fc = null;

	}

	/**
	 * Open streams to the file. InputStream w/o offset, OutputStream to append
	 * to the file can be set by boolean argument
	 * 
	 * @throws IOException
	 */

	public void open(boolean writeAppend) throws IOException {
		fc = getFileConnection();
		if (!writeAppend)
			fc.truncate(0);
		is = fc.openInputStream();
		os = fc.openOutputStream(fc.fileSize());

	}

	public int read(byte[] buffer) throws IOException {
		
		return is.read(buffer);
	}

	public void delete() throws IOException {
		close();
		fc = (FileConnection) Connector.open(rootName + pathName + fileName);
		if (fc.exists())
				fc.delete();
	
		
	}

} 

Author

Markus's picture
Markus