remove NEWLINE at the end of the file

G

gk

i use the code below to read a text file.

try {
BufferedReader in = new BufferedReader(new
FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}


but when the file has a NEWLINE at the END ....the code also read that.

i just dont want to read that NEWLINE as soon as i come to the end.

what i have to do ?
 
R

Roedy Green

but when the file has a NEWLINE at the END ....the code also read that.

i just dont want to read that NEWLINE as soon as i come to the end.

you mean you are getting a "" on the tail end of the file?

You may have multiple nls on the end of your file. Check with a hex
editor and chop them.

You may also just ignore 0-length lines.
 
G

gk

You may have multiple nls on the end of your file.....Check with a hex editor and chop them.


cant i do it by programatically ?

can i chop before reading the file by programatically ? How ?
 
R

Roedy Green

can i chop before reading the file by programatically ? How ?

You can write a tidier that chops blank lines off the tail end of the
file. Whenever you read a blank line, increment a counter. Whenever
you hit an non blank line, emit counter blank lines , zero the counter
then emit the line.
 
K

Knute Johnson

gk said:
i use the code below to read a text file.

try {
BufferedReader in = new BufferedReader(new
FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
process(str);
}
in.close();
} catch (IOException e) {
}


but when the file has a NEWLINE at the END ....the code also read that.

i just dont want to read that NEWLINE as soon as i come to the end.

what i have to do ?

If you know that you will never have a line in your data that is empty
then just ignore it;

while ((str = in.readLine()) != null) {
if (!str.equals(""))
process(str);
 
G

gk

I think this will look like much more impressive

while ((str = in.readLine()) != null &&(!str.equals("")) {
//if (!str.equals(""))
process(str);

Thnak you
 
G

Gordon Beaton

I think this will look like much more impressive

while ((str = in.readLine()) != null &&(!str.equals("")) {
//if (!str.equals(""))
process(str);

If your goal is to impress, why stop there when you can put
*everything* in the loop header?

while ((str = in.readLine()) != null && (!str.equals("") && (process(str) || true)));

/gordon

(See also http://www.m-w.com/cgi-bin/dictionary?va=irony)
 
G

gk

I apologige , if the adjective has hurt you. I understand, that was the
improper usage after all. english is not my native language.

I wanted to mean, how the code looks like if i put that way. Of course,
the code has been borrowed from you but customized.

language should not taken into account.

thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top