×
Namespaces

Variants
Actions
(Difference between revisions)

How to write data to a file in Java ME

From Nokia Developer Wiki
Jump to: navigation, search
ck.umraliya (Talk | contribs)
m
bogdan.galiceanu (Talk | contribs)
m (Reverted edits by Ck.umraliya (Talk); changed back to last version by jaaura )
Line 1: Line 1:
[[Category:Qt]][[Category:Qt for S60]][[Category:How To]][[Category:Code Examples]]
+
[[Category:Java]][[Category:Java ME]][[Category:How To]][[Category:Code Examples]][[Category:Files/Data]][[Category:File Connection and PIM API (JSR-75)]]
__NOTOC__
+
__NOEDITSECTION__
+
 
{{CodeSnippet
 
{{CodeSnippet
 
|id=...
 
|id=...
Line 81: Line 79:
 
==See also==
 
==See also==
  
* [[How to list root file systems in Java ME]]
+
* [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 files and folders 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 read an image from Gallery 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]

Revision as of 08:11, 20 March 2009

Article Metadata
Tested with
Devices(s): Emulator
CompatibilityArticle
Keywords: textBox,Commndbutton,String,FileConnection
Created: (28 Jan 2009)
Last edited: bogdan.galiceanu (20 Mar 2009)

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.


The full source code for a test MIDlet:

Source code: WriteMIDlet.java

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

500 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.

×