• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deleting the first line from a text file

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am want to delete the first line of text file using RandomAccessFile class.
My Input file "bulkwfids.txt"
has the data as below:
234567
345578
455678
566667

Expected output needs to be(first line deleted):
345578
455678
566667

But the actual ouput I am getting is as follows:
78
56
345578
455678
566667

Could someone please clear the doubt.
Here is the code snippet I have written:

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about reading one line at a time using buffered reader and then ignore the first line and print rest of the lines.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your modified file will still be the original length and contain the same characters except where you wrote over them. Is that what you want?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
setLength can help with that.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are incrementing n by 6 and not taking into account the \n or \n\r character(s).
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:setLength can help with that.



Just setLength() would cut off the last N characters - nothing to do with "deleting the first line from a file.

Fastest way to do this is to read the whole file into a byte[], step through the characters til you find the start of the 2nd line and rewrite from the byte[] over the old file.
THEN use setLength to clean up the end.

Bill
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for not noticing before but why are you using a random access file for text?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Brogden wrote:

Rob Spoor wrote:setLength can help with that.



Just setLength() would cut off the last N characters - nothing to do with "deleting the first line from a file.


I know, I was referring to your comment:

Your modified file will still be the original length and contain the same characters except where you wrote over them.


Perhaps I should have quoted that before.
reply
    Bookmark Topic Watch Topic
  • New Topic