• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Writing from a midlet to a file  RSS feed

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used the follwing program to write from a midlet to a file....the midlet compiles properly ......but when i run the midlet it throws the error saying " Cannot access file" ......how do i make this midlet to write to the file???
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
public class FC extends MIDlet implements CommandListener {
private Command exit, start;
private Display display;
private Form form;
public FC()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.EXIT, 1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp() throws MIDletStateChangeException
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (command == start)
{
try
{
OutputConnection connection = (OutputConnection)
Connector.open("file://c:/myfile.txt;append=true", Connector.WRITE );
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream( out );
output.println( "This is a test." );
out.close();
connection.close();
Alert alert = new Alert("Completed", "Data Written", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( ConnectionNotFoundException error )
{
Alert alert = new Alert(
"Error", "Cannot access file.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( IOException error )
{
Alert alert = new Alert("Error", error.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
}
}

Build complete

Running with storage root DefaultColorPhone

java.lang.ClassNotFoundException: com/sun/midp/io/j2me/file/Protocol

at javax.microedition.io.Connector.openPrim(+99)

at javax.microedition.io.Connector.open(+15)

at javax.microedition.io.Connector.open(+6)

at greeting.FC.commandAction(+34)

at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)

at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)

at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)

at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)

at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)

java.lang.ClassNotFoundException: com/sun/midp/io/j2me/file/Protocol

at javax.microedition.io.Connector.openPrim(+99)

at javax.microedition.io.Connector.open(+43)

at javax.microedition.io.Connector.open(+6)

at greeting.FC.commandAction(+34)

at javax.microedition.lcdui.Display$DisplayAccessor.commandAction(+282)

at javax.microedition.lcdui.Display$DisplayManagerImpl.commandAction(+10)

at com.sun.midp.lcdui.DefaultEventHandler.commandEvent(+68)

at com.sun.midp.lcdui.AutomatedEventHandler.commandEvent(+47)

at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+250)
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This won't work. MIDlets don't support the file:// scheme. Besides, what device has a "c:" drive? Where did you expect that file to go?

To store data from a MIDlet, use J2ME's Record Management System (RMS).

William Frantz
http://SprintDevelopers.com
 
pallavi utukuri
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
am using just j2me emulator to write to a file in my c drive......this example is from j2me complete reference.....anyways i will try RMS thanks
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
your question is write u can use that file connector. but that is supported by the new version of the sun j2me toolkit. that program u can run that and that file will be store on appdb folder of the toolkit,

I hope m clear.

Jignesh
jig2nesh@hotmail.com
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!