×
Namespaces

Variants
Actions
(Difference between revisions)

How to write data to a file in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
jarmlaht (Talk | contribs)
m
hamishwillee (Talk | contribs)
m (Hamishwillee - Fix categories)
 
(12 intermediate revisions by 5 users not shown)
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: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]][[Category:Files/Data on Java ME]][[Category:File Connection and PIM API (JSR-75)]][[Category:Code Examples]][[Category:How To]][[Category:Series 40]][[Category:Symbian]][[Category:Nokia Asha]]
{{CodeSnippet
+
{{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.}}
|id=...
+
{{ArticleMetaData <!-- v1.3 -->
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
+
|sourcecode= [[Media:WriteDataExample_src.zip]]
|devices= Emulator
+
|installfile= [[Media:WriteDataExample_bin.zip]]
|category=J2ME
+
|devices= Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
|subcategory=
+
|sdk= Nokia Asha SDK 1.0 (beta), [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.1 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java (beta)], [http://www.developer.nokia.com/info/sw.nokia.com/id/ec866fab-4b76-49f6-b5a5-af0631419e9c/S60_All_in_One_SDKs.html/ Nokia Symbian SDKs]
|creationdate=28 January 2009
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|keywords=textBox,Commndbutton,String,FileConnection
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Required capabilities for code (e.g. ID_CAP_LOCATION, ID_CAP_NETWORKING) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only - not link -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= [[User:skalogir]]
 +
|review-timestamp= 20130509
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090128
 +
|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.*;
Line 52: Line 58:
 
             String url = path + name;
 
             String url = path + name;
 
             String string = textbox.getString();
 
             String string = textbox.getString();
             byte data[] = string.getBytes();
+
             byte data[] = string.getBytes("UTF-8");
 
             FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
 
             FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
 
             if (!fconn.exists()) {
 
             if (!fconn.exists()) {
Line 79: Line 85:
 
==See also==
 
==See also==
  
* [http://wiki.forum.nokia.com/index.php/How_to_list_root_file_systems_in_Java_ME How to list root file systems in Java ME]
+
* [[How to list root file systems in Java ME]]
* [http://wiki.forum.nokia.com/index.php/How_to_list_files_and_folders_in_Java_ME How to list files and folders in Java ME]
+
* [[How to list files and folders in Java ME]]
* [http://wiki.forum.nokia.com/index.php/How_to_read_an_image_from_Gallery_in_Java_ME How to read an image from Gallery in Java ME]
+
* [[How to read an image from Gallery in Java ME]]
 +
<!-- Translation --> [[pt:Como gravar dados em um arquivo em Java ME]]

Latest revision as of 04:28, 26 July 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 ExampleTested with
Devices(s): Nokia Asha 501, Nokia C3-01, Nokia Asha 306, Nokia E7-00
CompatibilityArticle
Created: jarmlaht (28 Jan 2009)
Reviewed: skalogir (09 May 2013)
Last edited: hamishwillee (26 Jul 2013)

[edit] 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();
}
}

[edit] See also

This page was last modified on 26 July 2013, at 04:28.
427 page views in the last 30 days.
×