This discussion is archived
1 Reply Latest reply: Dec 4, 2010 10:40 AM by DarrylBurke RSS

Append text in file. and save it

821106 Newbie
Currently Being Moderated
Append text in file.

hi!
i am new j2me Programer.

1.i want to add(APPEND) the text in the file. i take this code form internet but i could succed Please help me.

2.i want to save it in other directory like if i made folder on mobile device with name c:\Old i want to save file in that how could i ?

kindly mention the Mistake.





package FileConnection;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;

/**
* @author QTracker
*/
public class FileConnection extends MIDlet implements CommandListener {

private boolean midletPaused = false;

private Command exit, start;
private Display display;
private Form form;
public FileConnection ()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.EXIT, 1);
form = new Form("Write To File");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}

private void initialize() { }
public void startMIDlet() { }
public void resumeMIDlet() {/
}
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {

Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
}
public Display getDisplay () {
return Display.getDisplay(this);
}


public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
midletPaused = true;
}


public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d) {
if (c == exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == start)
{
try
{
OutputConnection connection = (OutputConnection)
Connector.open("file:\\c:\\myfile.txt;append=true", Connector.WRITE );
OutputStream out = connection.openOutputStream();
PrintStream output = new PrintStream( out );
output.println( "This is a test." );
out.close();
connection.close();
Alert alert = new Alert("Completed", "Data Written", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( ConnectionNotFoundException error )
{
Alert alert = new Alert(
"Error", "Cannot access file.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
catch( IOException error )
{
Alert alert = new Alert("Error", error.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
}

}
  • 1. Re: Append text in file. and save it
    DarrylBurke Guru Moderator
    Currently Being Moderated
    | Moderator advice: | Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose. |
    | | Please don't post in threads that are long dead and don't hijack another poster's thread. |
    | | Please don't solicit off forum communication by email. |
    | Moderator action: | The other four posts you made have been deleted. |

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points