I'd like to modify the first line or header, of an existing csv file by adding a string to the end of that line.
I've tried using BufferedWriter to do so, but I can only get it to append at the end of the file.
My working code:
public static void writeStringtoCsvFile(String filePath, String input) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
out.append(input);
out.close();
}
It seems OpenCsv doesnt have an available method to append to an existing file either. The file I need to process is at least 160mb or 1+mil records in size, would the alternative method:
- BufferedReader to read all the lines
- Append whatever is needed to first line
- BufferedWriter to write everything to a new file
be too slow? Is there a more elegant solution to this? Thanks!
RandomAccessFileand overwrite some of the extra bytes.