Ray da Costa

« Teste de Software | Home | Aumentar memória heap para executar NetBeans »

Salvar arquivo txt em J2ME

de raydacosta | Terça, 11 de Agosto de 2009

http://bp3.blogger.com/_QFsoW7otbBM/RmsnqB5iqnI/AAAAAAAAAAU/rNxCq7upk_U/s1600-h/exemploFileSystem.JPG

JSR-75 = FileConnection!

FileConnection fc = (FileConnection) Connector.open(caminhoDoArquivo);

String dirPhotos = System.getProperty("fileconn.dir.photos");
String fileName = dirPhotos + "imagem.jpeg";
file = (FileConnection) Connector.open(fileName, Connector.READ_WRITE);
if (! file.exists()) {
file.create();
}
outStream = file.openOutputStream();
outStream.write(imagem);

Mais detalhado

private void save(String path, String name, String string) {
try {
String url = path + name;
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("Exceção de segurança:" + se.getMessage());
}
}

public void open(String fileName) {
try {
FileConnection fc = (FileConnection)
Connector.open( path + fileName );
if(!fc.exists()) {
throw new IOException("File não existe");
}
InputStream is = fc.openInputStream();
byte b[] = new byte[1024];
int length = is.read(b, 0, 1024);
textBox.setString( new String(b, 0, length) );
is.close();
fc.close();
}
catch (IOException ioe) {
System.out.println("IOException: "+ioe.getMessage());
}
catch (SecurityException se) {
System.out.println("Exceção de segurança:" + se.getMessage());
}

}

Categorias: J2ME |  | Enviar por e-mail  | Hits para esta publicação: 581

Um comentário para “ Salvar arquivo txt em J2ME ”

  1. Floost Says:
    Terça, 11 de Agosto de 2009 at 15:55

    Valuable thoughts and advices. I read your topic with great interest.

Deixe uma resposta.