• 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: 2112
16
  • 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
Marshal
Pie
Posts: 22124
88
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
Saloon Keeper
Posts: 18367
56
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic