Howto read newline character  
Author Message
Canned





PostPosted: 2007-8-3 17:05:00 Top

java-programmer, 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
 
Andrew Thompson





PostPosted: 2007-8-3 20:57:00 Top

java-programmer >> Howto read newline character Canned wrote:
>Hi,
>I'm new to java and I'm writing a simple text editor using swt examples
>from eclipse.

You are ..
- In way over your head.
- Using an API (SWT) that few people are familiar with.

>...The only problem I see ...

Wait till you become a little more experienced. ;-)

>..is that my editor wont display the
>text file correctly. It will display the whole text file in one line.

Yes. That is obvious from the code*.

>Here is the code I use to open file. I don't know where to start.

Start here <http://java.sun.com/docs/books/tutorial/>

...
> try {

Please refrain from posting 'tab' characters to usenet.

> do {
> if (textString != null) {
> sb.append(textString);
> }
> } while ((textString = fileInput.readLine()) != null);

* Here the code reads an entire line, stopping at the
next 'newline' character. The newline character itself
is not included. This can be confirmed by printing
the line to the standard out enclosed in quotes.
This is a basic debugging technique - look carefully
at the data that is being received.

To correct the problem, try something like, instead of..
sb.append(textString);
.put..
sb.append(textString + System.getProperty("line.separator") );

The purpose of calling getProperty for the line separator
is to obtain a line separator appropriate for the OS, which
is different for Windows, *nix and Mac.

Having siad all that, I suspect you are better off avoiding
GUI'd applications until you have a better grasp of the
(non GUI) basics and debugging techniques.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via http://www.javakb.com

 
Canned





PostPosted: 2007-8-3 22:00:00 Top

java-programmer >> Howto read newline character Andrew Thompson schreef:
> Canned wrote:
>> Hi,
>> I'm new to java and I'm writing a simple text editor using swt examples
>>from eclipse.
>
> You are ..
> - In way over your head.
> - Using an API (SWT) that few people are familiar with.
>
>> ...The only problem I see ...
>
> Wait till you become a little more experienced. ;-)
>
Having a boring vacation, I decided to learn something new. I already
know a little about Java so I decided to write something useful. I
decided to use SWT just because it looks nicer than awt/swing. I guess
you're right, I should start with something simple. :P

>> ..is that my editor wont display the
>> text file correctly. It will display the whole text file in one line.
>
> Yes. That is obvious from the code*.
>
>> Here is the code I use to open file. I don't know where to start.
>
> Start here <http://java.sun.com/docs/books/tutorial/>
>
I've read that already but not all of them.
> ...
>> try {
>
> Please refrain from posting 'tab' characters to usenet.
>
>> do {
>> if (textString != null) {
>> sb.append(textString);
>> }
>> } while ((textString = fileInput.readLine()) != null);
>
> * Here the code reads an entire line, stopping at the
> next 'newline' character. The newline character itself
> is not included. This can be confirmed by printing
> the line to the standard out enclosed in quotes.
> This is a basic debugging technique - look carefully
> at the data that is being received.
>
> To correct the problem, try something like, instead of..
> sb.append(textString);
> .put..
> sb.append(textString + System.getProperty("line.separator") );
>
> The purpose of calling getProperty for the line separator
> is to obtain a line separator appropriate for the OS, which
> is different for Windows, *nix and Mac.
>
> Having siad all that, I suspect you are better off avoiding
> GUI'd applications until you have a better grasp of the
> (non GUI) basics and debugging techniques.
>


Thanks for your explain. It works. :D
 
 
Lew





PostPosted: 2007-8-4 6:05:00 Top

java-programmer >> Howto read newline character Andrew Thompson schreef:
>> To correct the problem, try something like, instead of..
>> sb.append(textString);
>> .put..
>> sb.append(textString + System.getProperty("line.separator") );

Or for a slight efficiency improvement:
sb.append( textString ).append( System.getProperty("line.separator") );

By the way, to include the type of a variable in its name, as "textString", is
not helpful for most purposes. Suppose you abstracted that variable to
CharSequence, or decided to make it a StringBuilder? You'd have to change its
name, which is silly.

Names should reflect the problem-domain logic, not the computer-domain
implementation.

--
Lew
 
 
Canned





PostPosted: 2007-8-4 7:27:00 Top

java-programmer >> Howto read newline character Lew schreef:
> Andrew Thompson schreef:
>>> To correct the problem, try something like, instead of..
>>> sb.append(textString);
>>> .put..
>>> sb.append(textString + System.getProperty("line.separator") );
>
> Or for a slight efficiency improvement:
> sb.append( textString ).append( System.getProperty("line.separator") );
>
> By the way, to include the type of a variable in its name, as
> "textString", is not helpful for most purposes. Suppose you abstracted
> that variable to CharSequence, or decided to make it a StringBuilder?
> You'd have to change its name, which is silly.
>
> Names should reflect the problem-domain logic, not the computer-domain
> implementation.
>
I'm aware of that problem but because this is just a simple program I
decided to write my variables with the name of its type, so I know that
text variable holds text type, shell holds shell, and so on ....It is
easier for me to read.