import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.Enumeration; import java.io.IOException; import java.io.DataOutputStream; import javax.microedition.io.file.*; import javax.microedition.io.*; public class Midlet extends MIDlet implements CommandListener { private List rootList; private Form editMap; private ChoiceGroup mapType; private Command ok; private Command save; private Command exit; private String path; public void startApp() { rootList = new List("Выберите папку с картой", List.IMPLICIT); ok = new Command("Выбрать", Command.OK, 0); save = new Command("Сохранить", Command.OK, 0); exit = new Command("Выход", Command.EXIT, 0); editMap = new Form("Параметры карты"); try{ Enumeration e = FileSystemRegistry.listRoots(); while(e.hasMoreElements()){ rootList.append(e.nextElement().toString(), null); } }catch(SecurityException se){ } rootList.addCommand(ok); rootList.addCommand(exit); rootList.setCommandListener(this); Display.getDisplay(this).setCurrent(rootList); } public void commandAction(Command c, Displayable d){ int selindex = rootList.getSelectedIndex(); path = "file://" + rootList.getString(selindex) + "world.sav"; byte b; if(c==ok){ changeForm(editMap); } if(c==save){ b = Byte.parseByte(new Integer(mapType.getSelectedIndex()).toString()); try{ writeWorld(path, b); }catch(IOException ioe){ } } if(c==exit){ destroyApp(true); notifyDestroyed(); } } public void changeForm(Form f){ mapType = new ChoiceGroup("Тип карты", ChoiceGroup.IMPLICIT); mapType.append("Выживание", null); mapType.append("Творчество", null); f.append(mapType); f.addCommand(save); f.addCommand(ok); f.setCommandListener(this); Display.getDisplay(this).setCurrent(f); } public void writeWorld(String path, byte b) throws IOException{ byte[] ba = {b}; int off = 1; FileConnection fc = (FileConnection)Connector.open(path); if(fc.exists()){ DataOutputStream dos = fc.openDataOutputStream(); dos.write(ba, off, off); dos.close(); } else{ throw new IOException("File doesn't exists"); } fc.close(); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }