I am sorry if my english is not so good, but I am doing text editor application on my mobile (Nokia 9500) and I am facing two problems:

1) Getting '?' instead of newline in the saved file. How can I manage to save a text file from TextArea?

try {
String text = textArea.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(text);
bw.flush();
bw.close();
} catch (IOException e) { }

2) Getting an error that terminate the application when I am trying to cut a spicific text. I do not know why I am getting this error!

private void cut(){
copy();
int start=textArea.getSelectionStart();
int end=textArea.getSelectionEnd();
textArea.replaceRange("",start,end);
notify(cutString);
}

here is the my code:

/*
* TextEditor.java
* @author Ahmed Hashim
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;

// Symbian S80 Extensions API
import com.symbian.epoc.awt.CBAListener;
import com.symbian.epoc.awt.CBAEvent;
import com.symbian.epoc.awt.EikCommandButtonGroup;
import com.symbian.epoc.awt.EikStatusPane;
import com.symbian.epoc.awt.EikVirtualCursor;

public class TextEditor extends Frame implements WindowListener,ActionListener,
ClipboardOwner,KeyListener,CBAListener {

private final int NOACTION = 0;
private final int NEWFILE = 1;
private final int OPENFILE = 2;
private final int EXIT = 3;

private final String readyString = new String("Ready");
private final String newString = new String("New");
private final String openString = new String("Open");
private final String saveString = new String("Save");
private final String saveAsString = new String("Save As");
private final String exitString = new String("Exit");
private final String cutString = new String("Cut");
private final String copyString = new String("Copy");
private final String pasteString = new String("Paste");
private File file = new File("Untitled.txt");
private boolean neverNamed = true;
private boolean dirty = false;

private Button saveButton;
private Button discardButton;
private Button cancelButton;
private Button exitButton;
private Menu fileMenu;
private Menu editMenu;
private TextArea textArea;
private Label label;

private EikCommandButtonGroup cbg;
private int beginIndex = 0;


public static void main(String arg[]){
new TextEditor();
}

private void setTitle() {
super.setTitle("Text Editor - " + file.getName());
}

public TextEditor() {
System.out.println(System.getProperty("java.class.path"));

setTitle();
setLayout(new BorderLayout());

textArea=new TextArea("",5,15);
add(textArea,BorderLayout.CENTER);

label=new Label(readyString);
add(label,BorderLayout.SOUTH);

MenuBar menuBar = new MenuBar();

fileMenu = new Menu("File");
fileMenu.add(newString);
fileMenu.add(openString);
fileMenu.add(saveString);
fileMenu.add(saveAsString);
fileMenu.add(exitString);

editMenu = new Menu("Edit");
editMenu.add(cutString);
editMenu.add(copyString);
editMenu.add(pasteString);

menuBar.add(fileMenu);
menuBar.add(editMenu);
setMenuBar(menuBar);

cbg = new EikCommandButtonGroup();
cbg.setText(EikCommandButtonGroup.BUTTON1, openString);
cbg.setText(EikCommandButtonGroup.BUTTON2, saveString);
cbg.setText(EikCommandButtonGroup.BUTTON3, "");
cbg.setText(EikCommandButtonGroup.BUTTON4, exitString);
add(cbg,BorderLayout.EAST);

textArea.addKeyListener(this);
fileMenu.addActionListener(this);
editMenu.addActionListener(this);;
cbg.addCBAListener(this);
addWindowListener(this);

EikStatusPane.setStatusPaneStyle(EikStatusPane.NONE);
EikVirtualCursor.setState(EikVirtualCursor.ON);

show();
}


public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if (cmd.equals(newString))
newFile();
else if (cmd.equals(openString))
openFile();
else if (cmd.equals(saveString))
saveFile(false);
else if (cmd.equals(saveAsString))
saveFile(true);
else if (cmd.equals(exitString))
exit();
else if (cmd.equals(cutString))
cut();
else if (cmd.equals(copyString))
copy();
else if (cmd.equals(pasteString))
paste();
}


public void cbaActionPerformed(CBAEvent cba) {
if (cba.getID() == EikCommandButtonGroup.BUTTON1) openFile();
else if (cba.getID() == EikCommandButtonGroup.BUTTON2) saveFile(false);
else if (cba.getID() == EikCommandButtonGroup.BUTTON4) exit();
}


private boolean showFileDialog(int mode){

FileDialog fileDialog = new FileDialog(this);

if (mode==FileDialog.LOAD){
fileDialog.setTitle(openString);
}
else{
fileDialog.setTitle(saveString);
}

fileDialog.setMode(mode);
fileDialog.setFile(file.getName());

fileDialog.show();

String fileName = fileDialog.getFile();
String pathName = fileDialog.getDirectory() + fileName;

fileDialog.dispose();

if (fileName != null){
file = new File(pathName);
return true;
}
else
return false;
}


private void notify(String msg){
label.setText(msg);
}

private void newFile() {

neverNamed = true;
dirty = false;
file = new File("Untitled.txt");
this.setTitle();
textArea.setText("");
notify(readyString);
}

private void openFile() {

String line;
String newLine=System.getProperty("line.separator");
notify(openString);

if (showFileDialog(FileDialog.LOAD)){
try {
BufferedReader br = new BufferedReader(new FileReader(file));

while ( (line=br.readLine()) != null)
{
textArea.append(line+newLine);
}
br.close();
neverNamed = false;
dirty = false;
this.setTitle();
notify(readyString);

}catch (IOException e) {
notify("Error - could not read '"+file.getName()+"' file!");
System.out.println(e.toString());
}
}

}

private void saveFile(boolean As){

boolean ok;

notify(saveString);

if (neverNamed || As) {
ok = showFileDialog(FileDialog.SAVE);
neverNamed = false;
}
else
ok = true;

if (ok) {
try {
String text = textArea.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(text);
bw.flush();
bw.close();
notify(readyString);
neverNamed = false;
dirty = false;
this.setTitle();

}

catch (IOException e) {
notify("Error - could not write to '"+file.getName()+"' file!");
}
}
}


private void exit(){
setVisible(false);
dispose();
System.exit(0);
}

private void cut(){
copy();
int start=textArea.getSelectionStart();
int end=textArea.getSelectionEnd();
textArea.replaceRange("",start,end);
notify(cutString);


}

private void copy() {
String selection = textArea.getSelectedText();
if (selection != null){
StringSelection contents = new StringSelection(selection);
getToolkit().getSystemClipboard().setContents(contents, this);
notify(copyString);
}

}

private void paste() {

Transferable contents =getToolkit().getSystemClipboard().getContents( this );

if ( contents != null ) {
try {
String selection = (String)contents.getTransferData(DataFlavor.stringFlavor);
if (selection != null)
textArea.insert(selection, textArea.getCaretPosition());
notify(pasteString);
} catch ( Exception e ) {
notify("Error - could not paste the text content!");
System.out.println(e.toString());
}
}
}


public void keyTyped(KeyEvent e) {
dirty = true;
notify(readyString);
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e){}
public void windowClosing(WindowEvent windowEvent){ exit(); }
public void lostOwnership(Clipboard c,Transferable t){}
public void windowActivated(WindowEvent windowEvent) {}
public void windowClosed(WindowEvent windowEvent) {}
public void windowDeactivated(WindowEvent windowEvent) {}
public void windowDeiconified(WindowEvent windowEvent) {}
public void windowIconified(WindowEvent windowEvent) {}
public void windowOpened(WindowEvent windowEvent) {}

}