开发者

Remove first line from delimited file

开发者 https://www.devze.com 2023-03-20 06:47 出处:网络
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.

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 fi开发者_开发百科gure it out , as to how to get the length of the first line and delete it.


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.


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


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消