1

I have a delimited file which can contain around millions of records , now I want to delete the first line from the delimited file before processing it further.

The length of the first line is variable , it differs as per the file type.. now I have done a readup on the FileChannel and RandomAccessFile which have been suggested as the best ways to delete the first line.

But I am unable to figure it out , as to how to get the length of the first line and delete it.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Vivek
  • 41
  • 1
  • 5
  • 1
    very basic one apparently is it CSV file . Try Google for opencsv . When reading the file just disregard first line whats the issue – Shahzeb Jul 15 '11 at 07:05
  • well... its not a CSV file.. its a pipe delimited text file..my mistake.. i did not mention the same... – Vivek Jul 15 '11 at 07:35

4 Answers4

4

Don't delete it, just read-and-ignore.

If you have to prepare the file because the file processing units can't handle a file with an incorrect first line, then you'll have to read and rewrite it. There is no I/O operation available that can delete contents from file in the filesystem.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

use readLine() to read line by line , just ommit first line and consider others in processing

jmj
  • 237,923
  • 42
  • 401
  • 438
  • but wudn't the buffered reader be a bit heavy.. considering the file has millions of records... reading them.. omitting the first line.. and the re-writing the file back wud be an issue ... right ?? – Vivek Jul 15 '11 at 06:45
3

Thanks for the inputs. Depending on the same , I figured out a solution to remove the first line from the delimited pipe file.

Mentioned below is a code snippet

RandomAccessFile raf = new RandomAccessFile("path to ur delimited file", "rw");
FileChannel fileChannel = raf.getChannel(); 
raf.readLine();     
raf.seek(raf.getFilePointer());         
int len = (int) (raf.length() - raf.getFilePointer());
byte[] bytearr = new byte[len];         
raf.readFully(bytearr, 0, len);         
fileChannel.truncate(0);            
raf.write(bytearr,0,len);
Vivek
  • 41
  • 1
  • 5
0

You could use a BufferedReader and use BufferedReader.readLine() to "delete" the first line before processing. From here you could continue to process the rest of the lines or store them into a file to process later. The latter option might not be the most efficient option available to you.

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • yes ... i agree with you.. the latter option wud not be a very efficient option...considering the rows in the file.. that's why had opted for the FileChannel / RandomAccessFile... but can't figure out as how to determine the end of the line.. Mayb wud have to read the bytes ... and after the end of the first line.. move the pointer below.. hwz this ?? – Vivek Jul 15 '11 at 06:47
  • A new line is determined by the System property "line.separator" and will be `\n\r`, `\r\n`, `\n`, or `\r` depending on what your operating system uses (the supplied being the most common). – Jeffrey Jul 15 '11 at 16:58