Pessoal, bom dia!
Então estou fazendo a criação de um arquivo txt para um celular qualquer. O código abaixo cria o arquivo txt mas eu queria uma maneira genérica para pegar o diretório atual. Vejam a linha: saveFile(“file:///tflash/Other files/”, “teste.txt”, “blabla”);. Pois então queria uma maneira de pegar o diretório atual onde ele foi instalado, para que a aplicação consiga achar o diretório e consiga salvar em qualquer aparelho móvel. Obrigado.
[code]package servico;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.;
import javax.microedition.midlet.;
public class Pedidos extends MIDlet implements CommandListener{
private Command cmdSend;
private Command sair = new Command(“Sair”, Command.EXIT, 0);
private Command okCommand = new Command(“Sim”, Command.OK, 0);
private Command cancelCommand = new Command(“Não”, Command.STOP, 0);
private Display display;
private Form tela;
private Alert alerta = new Alert("", “Salvar?”, null, AlertType.CONFIRMATION);
private TextBox textBox;
private String path;
public Pedidos(){
cmdSend = new Command(“Enviar”, Command.SCREEN, 0 );
display = Display.getDisplay( this );
tela = new Form(“Requisição de pedidos”);
tela.addCommand( sair );
tela.addCommand( cmdSend );
alerta.addCommand(okCommand);
alerta.addCommand(cancelCommand);
tela.setCommandListener(this);
alerta.setCommandListener(this);
}
public void startApp(){
display = Display.getDisplay(this);
display.setCurrent( tela );
tela.setCommandListener(this);
}
public void pauseApp(){
}
public void destroyApp(boolean b){
display.setCurrent( null );
this.notifyDestroyed();
}
public void commandAction( Command command, Displayable displayable){
if (command == sair) {
destroyApp(true);
}
if ( command == cmdSend )
{
display.setCurrent(alerta);
}
if(command == okCommand){
display.setCurrent(tela);
Thread teste = new Thread(){
public void run(){
saveFile("file:///tflash/Other files/", "teste.txt", "blabla");
}
};
teste.start();}
if(command == cancelCommand)
{
...
}
}
//salvar
private void saveFile(String path, String name, String string) {
try {
String url = path + name;
byte data[] = string.getBytes();
FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
System.out.println(fconn.isDirectory());
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());
}
}
}
[/code]