×
Namespaces

Variants
Actions
(Difference between revisions)

How to write data to a file in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Mark as verified on Nokia Asha DP 1.0)
skalogir (Talk | contribs)
(Skalogir -)
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:Series 40]][[Category:Symbian]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]][[Category:Nokia Asha]][[Category:Nokia Asha Developer Platform 1.0]]
+
[[Category:Java ME]][[Category:How To]][[Category:Code Examples]][[Category:Files/Data]][[Category:File Connection and PIM API (JSR-75)]][[Category:Series 40]][[Category:Symbian]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Series 40 6th Edition FP1]][[Category:Series 40 Developer Platform 2.0]][[Category:Nokia Belle]][[Category:Nokia Asha Platform 1.0]]
 
{{Abstract|This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a {{Icode|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.}}
 
{{Abstract|This article shows, how to write data to a file by using FileConnection API (JSR-75). This example MIDlet has a {{Icode|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
 
|devices= Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
 
|devices= Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|platform= Nokia Asha DP1.0 and later, Series 40 DP 1.0 and later, Nokia Belle
+
|platform= Nokia Asha Platform 1.0 and later, Series 40 DP 1.0 and later, Nokia Belle
 
|creationdate=20090128
 
|creationdate=20090128
 
|keywords=textBox,Commndbutton,String,FileConnection
 
|keywords=textBox,Commndbutton,String,FileConnection

Revision as of 10:41, 13 May 2013

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.

Article Metadata
Code Example
Installation file: WriteDataExample binaries
Tested with
Devices(s): Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Keywords: textBox,Commndbutton,String,FileConnection
Created: jarmlaht (28 Jan 2009)
Reviewed: skalogir (09 May 2013)
Last edited: skalogir (13 May 2013)

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("UTF-8");
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

383 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×