Namespaces
Variants
Actions
(Difference between revisions)

How to write data to a file in Java ME

Jump to: navigation, search
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
m (Hamishwillee - Update ArticleMetaData)
Line 1: Line 1:
 
[[Category:Java ME]][[Category:How To]][[Category:Code Examples]][[Category:Files/Data]][[Category:File Connection and PIM API (JSR-75)]]
 
[[Category:Java ME]][[Category:How To]][[Category:Code Examples]][[Category:Files/Data]][[Category:File Connection and PIM API (JSR-75)]]
 +
{{Abstract|This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a TextBox, where text can be entered. Then the text can be saved to readme.txt file, which is in this case created under Gallery's Images folder.}}
 
{{ArticleMetaData
 
{{ArticleMetaData
|id=...
 
 
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition  
 
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition  
 
|devices= Emulator
 
|devices= Emulator
|category=J2ME
+
|creationdate=20090128
|subcategory=
+
|creationdate=28 January 2009
+
 
|keywords=textBox,Commndbutton,String,FileConnection
 
|keywords=textBox,Commndbutton,String,FileConnection
 
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
Line 14: Line 11:
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 
|author=[[User:Jarmlaht]]
 
|author=[[User:Jarmlaht]]
 
}}
 
}}
==Overview==
 
This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a TextBox, where text can be entered. Then the text can be saved to readme.txt file, which is in this case created under Gallery's Images folder.
 
 
  
 +
==Source code: WriteMIDlet.java==
 
The full source code for a test MIDlet:
 
The full source code for a test MIDlet:
 
==Source code: WriteMIDlet.java==
 
 
 
<code java>
 
<code java>
 
import javax.microedition.midlet.*;
 
import javax.microedition.midlet.*;

Revision as of 03:31, 18 November 2011

This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a TextBox, where text can be entered. Then the text can be saved to readme.txt file, which is in this case created under Gallery's Images folder.

SignpostIcon FloppyDisk 52.png
SignpostIcon Nokia Asha 52.png
Article Metadata

Tested with
Devices(s): Emulator

Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition

Article
Keywords: textBox,Commndbutton,String,FileConnection
Created: jarmlaht (28 Jan 2009)
Last edited: hamishwillee (18 Nov 2011)

Source code: WriteMIDlet.java

The full source code for a test MIDlet:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
 
public class WriteMIDlet extends MIDlet implements CommandListener {
private TextBox textbox;
private String photos = "fileconn.dir.photos";
private Command saveCommand;
private Command exitCommand;
private String path;
 
public void startApp() {
textbox = new TextBox("WriteMIDlet", "", 1000, TextField.ANY);
saveCommand = new Command("Save", Command.SCREEN, 1);
exitCommand = new Command("Exit", Command.EXIT, 1);
textbox.addCommand(saveCommand);
textbox.addCommand(exitCommand);
textbox.setCommandListener(this);
Display.getDisplay(this).setCurrent(textbox);
path = System.getProperty(photos);
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
private void saveFile(String path, String name) {
try {
String url = path + name;
String string = textbox.getString();
byte data[] = string.getBytes();
FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
}
OutputStream ops = fconn.openOutputStream();
ops.write(data);
ops.close();
fconn.close();
}
catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
}
catch (SecurityException se) {
System.out.println("Security exception:" + se.getMessage());
}
}
 
public void commandAction(Command c, Displayable d) {
if (c == saveCommand) saveFile(path, "readme.txt");
if (c == exitCommand) this.notifyDestroyed();
}
}

See also

738 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved