Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Take a look at this (write/read/delete) </p> <pre><code>class Scr extends MainScreen implements FieldChangeListener { ButtonField mWrite; ButtonField mRead; ButtonField mDelete; String mFileName = System.getProperty("fileconn.dir.photos") + "test.bmp"; public Scr() { mWrite = new ButtonField("Write file", ButtonField.CONSUME_CLICK); add(mWrite); mWrite.setChangeListener(this); mRead = new ButtonField("Read file", ButtonField.CONSUME_CLICK); add(mRead); mRead.setChangeListener(this); mDelete = new ButtonField("Delete file", ButtonField.CONSUME_CLICK); add(mDelete); mDelete.setChangeListener(this); } public void fieldChanged(Field field, int context) { if (mWrite == field) { byte[] bytes = new byte[] { 1, 2, 1, 1 }; writeFile(bytes, mFileName); Dialog.inform("File " + mFileName + " saved"); } else if (mRead == field) { byte[] bytes = readFile(mFileName); if (null != bytes) Dialog.inform("File " + mFileName + " opened"); } else if (mDelete == field) { deleteFile(mFileName); Dialog.inform("File " + mFileName + " deleted"); } } private void writeFile(byte[] data, String fileName) { FileConnection fconn = null; try { fconn = (FileConnection) Connector.open(fileName, Connector.READ_WRITE); } catch (IOException e) { System.out.print("Error opening file"); } if (fconn.exists()) try { fconn.delete(); } catch (IOException e) { System.out.print("Error deleting file"); } try { fconn.create(); } catch (IOException e) { System.out.print("Error creating file"); } OutputStream out = null; try { out = fconn.openOutputStream(); } catch (IOException e) { System.out.print("Error opening output stream"); } try { out.write(data); } catch (IOException e) { System.out.print("Error writing to output stream"); } try { fconn.close(); } catch (IOException e) { System.out.print("Error closing file"); } } private byte[] readFile(String fileName) { byte[] result = null; FileConnection fconn = null; try { fconn = (FileConnection) Connector.open(fileName, Connector.READ); } catch (IOException e) { System.out.print("Error opening file"); } if (!fconn.exists()) { Dialog.inform("file not exist"); } else { InputStream in = null; ByteVector bytes = new ByteVector(); try { in = fconn.openInputStream(); } catch (IOException e) { System.out.print("Error opening input stream"); } try { int c = in.read(); while (-1 != c) { bytes.addElement((byte) c); c = in.read(); } result = bytes.getArray(); } catch (IOException e) { System.out.print("Error reading input stream"); } try { fconn.close(); } catch (IOException e) { System.out.print("Error closing file"); } } return result; } private void deleteFile(String fileName) { FileConnection fconn = null; try { fconn = (FileConnection) Connector.open(fileName, Connector.READ_WRITE); } catch (IOException e) { System.out.print("Error opening file"); } if (!fconn.exists()) { Dialog.inform("file not exist"); } else { try { fconn.delete(); } catch (IOException e1) { System.out.print("Error deleting file"); } try { fconn.close(); } catch (IOException e) { System.out.print("Error closing file connection"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload