Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > remove NEWLINE at the end of the file

Reply
Thread Tools

remove NEWLINE at the end of the file

 
 
gk
Guest
Posts: n/a
 
      04-04-2006
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 ?

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-04-2006
On 3 Apr 2006 19:05:04 -0700, "gk" <(E-Mail Removed)> wrote, quoted or
indirectly quoted someone who said :

>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.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
gk
Guest
Posts: n/a
 
      04-04-2006

>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 ?

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-04-2006
On 3 Apr 2006 21:28:21 -0700, "gk" <(E-Mail Removed)> wrote, quoted or
indirectly quoted someone who said :

>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.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      04-04-2006
gk wrote:
> 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);

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
gk
Guest
Posts: n/a
 
      04-05-2006
I think this will look like much more impressive

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

Thnak you

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-05-2006
On 5 Apr 2006 09:20:18 +0100, Gordon Beaton <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>process(str) || true


what a strange way to code.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      04-05-2006
On 4 Apr 2006 18:09:59 -0700, gk wrote:
> 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)

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      04-05-2006
On Wed, 05 Apr 2006 08:07:56 GMT, Roedy Green wrote:
> what a strange way to code.


Follow the link I posted.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
gk
Guest
Posts: n/a
 
      04-11-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 13 09-21-2011 12:17 AM
Is a blank at the end of a C file a better style than a newline atthe end of the file or is it better to place none? Jimmy C Programming 3 09-09-2011 10:36 PM
Warning: no newline at end of file surendran.d@gmail.com C++ 8 01-12-2006 05:32 PM
warning: no newline at end of file padam.singh@gmail.com C Programming 2 06-13-2005 09:59 AM
newline at the end of file av C++ 1 11-20-2003 02:32 AM



Advertisments