Motorola Inc. | i88s J2ME Developers’ Guide |
| sc.close(); |
| } |
| catch(Exception ex) { |
| } |
} |
|
} |
|
private | void deleteFile() { |
try | { |
| //open a file in the delete mode |
//by doing this existing file is eventually delete sc =
(StreamConnection)Connector.open("file://"+"fileURL"+";"+"DELETE"); //close the stream
if (sc != null) sc.close();
myOutput.append("file deleted"); }catch (Exception ex1 ) {
System.out.println("Exception: " + ex1.getMessage()); try{
if (sc != null) sc.close();
}
catch(Exception ex) {
}
}
}
}
3.5Tips
•These are five basic steps to communicating with a file:
1.Open the file using the Connector.open() method of Connector class. This returns a StreamConnection object for file. Otherwise, an IOException is thrown.
2.Get the output stream using the openOutputStream() method of OutputConnection.
3.Get the input stream using the openInputStream() method of InputConnection.
4.Read and write data onto those streams.
5.Close the file using the close() method of Connection.
Once the connection has been established, simply use the normal methods of any input or output stream to read and write data.
•File I/O is a simple alternative to Record Management System (RMS). When used effectively, direct File I/O can speed up storing and retrieving data.
•After creating a file from a MIDlet suite, the file is associated with the current MIDlet suite only. No other MIDlet suite can access it.
Version 1.0 - Page 27