Howto read newline character
Hi,
I'm new to java and I'm writing a simple text editor using swt examples
from eclipse. The only problem I see is that my editor wont display the
text file correctly. It will display the whole text file in one line.
Here is the code I use to open file. I don't know where to start.
public class OpenFile extends SelectionAdapter {
public void widgetSelected(SelectionEvent event) {
FileDialog fileOpen = new FileDialog(shell, SWT.OPEN);
fileOpen.setText("Open");
String[] filterExt = { "*.txt", "*.ini", "*.*" };
fileOpen.setFilterExtensions(filterExt);
String selected = fileOpen.open();
if (selected == null) {
return;
}
FileReader file = null;
try {
file = new FileReader(selected);
} catch (FileNotFoundException e) {
MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
messageBox.setMessage("Could not open file.");
messageBox.setText("Error");
messageBox.open();
return;
}
BufferedReader fileInput = new BufferedReader(file);
String textString = null;
StringBuffer sb = new StringBuffer();
try {
do {
if (textString != null) {
sb.append(textString);
}
} while ((textString = fileInput.readLine()) != null);
} catch (IOException e1) {
MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR |
SWT.OK);
messageBox.setMessage("Could not write to file.");
messageBox.setText("Error");
messageBox.open();
return;
}
text.setText(sb.toString());
}
}
Thanks in advance,
C