/* * FileWriter.java * * Created on January 2, 2008, 11:14 AM */ package hello; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; import javax.microedition.io.*; import javax.microedition.io.file.FileConnection; /** * * @author Fiona */ public class FileWriter extends MIDlet implements CommandListener { /** * Creates a new instance of FileWriter */ public FileWriter() { } private Form helloForm; private StringItem helloStringItem; private Command exitCommand; private Command okCommand1; /** This method initializes UI of the application. */ private void initialize() { // Insert pre-init code here getDisplay().setCurrent(get_helloForm()); // Insert post-init code here } /** Called by the system to indicate that a command has been invoked on a particular displayable. * @param command the Command that ws invoked * @param displayable the Displayable on which the command was invoked */ public void commandAction(Command command, Displayable displayable) { // Insert global pre-action code here if (displayable == helloForm) { if (command == exitCommand) { // Insert pre-action code here exitMIDlet(); // Insert post-action code here } else if (command == okCommand1) { // Insert pre-action code here // Do nothing String v = System.getProperty("microedition.io.file.FileConnection.version"); if( v != null ){ System.out.println("FileConnection available"); } else { exitMIDlet(); } //String url = "file:///Phone memory/test2.txt"; String url = "file:///Memory card/test.txt"; FileConnection conn = null; try { conn = (FileConnection) Connector.open(url); if( !conn.exists() ){ // create the file conn.create(); OutputStream out = conn.openOutputStream(); // now write data to the file String s = "Grr argh"; out.write(s.getBytes()); out.flush(); out.close(); System.out.println("Bob"); } conn.close(); } catch( IOException e ){ // error exitMIDlet(); } catch( SecurityException e ){ // no permission to create/write exitMIDlet(); } // Insert post-action code here } } // Insert global post-action code here } /** * This method should return an instance of the display. */ public Display getDisplay() { return Display.getDisplay(this); } /** * This method should exit the midlet. */ public void exitMIDlet() { getDisplay().setCurrent(null); destroyApp(true); notifyDestroyed(); } /** This method returns instance for helloForm component and should be called instead of accessing helloForm field directly. * @return Instance for helloForm component */ public Form get_helloForm() { if (helloForm == null) { // Insert pre-init code here helloForm = new Form(null, new Item[] {get_helloStringItem()}); helloForm.addCommand(get_exitCommand()); helloForm.addCommand(get_okCommand1()); helloForm.setCommandListener(this); // Insert post-init code here } return helloForm; } /** This method returns instance for helloStringItem component and should be called instead of accessing helloStringItem field directly. * @return Instance for helloStringItem component */ public StringItem get_helloStringItem() { if (helloStringItem == null) { // Insert pre-init code here helloStringItem = new StringItem("Hello", "Hello, World!"); // Insert post-init code here } return helloStringItem; } /** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly. * @return Instance for exitCommand component */ public Command get_exitCommand() { if (exitCommand == null) { // Insert pre-init code here exitCommand = new Command("Exit", Command.EXIT, 1); // Insert post-init code here } return exitCommand; } /** This method returns instance for okCommand1 component and should be called instead of accessing okCommand1 field directly. * @return Instance for okCommand1 component */ public Command get_okCommand1() { if (okCommand1 == null) { // Insert pre-init code here okCommand1 = new Command("Ok", Command.OK, 1); // Insert post-init code here } return okCommand1; } public void startApp() { initialize(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }