问题
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.
回答1:
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.
回答2:
use readLine() to read line by line , just ommit first line and consider others in processing
回答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);
回答4:
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.
来源:https://stackoverflow.com/questions/6703501/remove-first-line-from-delimited-file