This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to delete empty line at the end of the file?

 
David Martínez
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!

I'm working with text files, In one point I need to modify one line of the text file

I'm using opencsv library to get this easily done, but once the modification is done, there is an empty line remaining at the end.



This is the code I'm using to update just one column of the line


This affects my following operations.
 
Dave Tolls
Ranch Hand
Posts: 2365
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like that's how CSVWriter works.
It seems to append a new line at the end of each line.
I can't see any flags you can use to ensure the last one does not have the new line.
 
Henry Wong
author
Sheriff
Posts: 22607
111
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the issue is only the extra newline at the end of the file, then, one possible option is to use the RandomAccessFile class to remove it -- by reopening the file, getting the length of the file, and setting the length to one (or two) bytes less. In my opinion, it's a bit awkward, but it's a quick and dirty workaround.

Henry
 
Tim Holloway
Bartender
Posts: 18465
61
Android Eclipse IDE Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of apps find it offensive if the last line of a text file is anomalous. They prefer that every line, including the last, should include a terminator.

In fact, let me go back to where it all started. On a Teletype[TM] printer, the control codes (Carriage Return, Line Feed) had physical consequences. So if you printed a file without that final CR/LF, then the print head would be left hanging out in space at the end of the last text line. That was usually not where you watned the next text to start printing.

Or. to put in another way, the newline character (or character sequence on WIndows, where a simple hint is not enough) is a line terminator, not a line separator. It's considered as part of the line text, not as meta-data. So conventionally, all lines in a text file should end with a newline, including the last one,
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!