Namespaces
Variants
Actions
Revision as of 02:21, 19 December 2011 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Como gravar dados em um arquivo em Java ME

Jump to: navigation, search


MultiMediaTile.png
Dados do artigo

Testado com
Aparelho(s): Emulador

Compatibilidade
Plataforma(s): S60 3ª Edição FP1, S60 3ª Edição FP2, S60 5ª Edição

Artigo
Palavras-chave: textBox,Commndbutton,String,FileConnection
Tradução:
Última alteração feita por hamishwillee em 19 Dec 2011

Resumo

Este artigo mostra, como gravar dados em um arquivo usando a API FileConnection (JSR-75). Este exemplo MIDlet tem um TextBox, onde o texto pode ser digitado. Em seguida, o texto pode ser salvo em arquivo readme.txt, que neste caso é criado sob a pasta da galeria de Imagens.


O código fonte completo para um teste MIDlet:

Código-fonte: 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();
}
}

Veja também

129 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