Here you can find the source of writeHeader(File input, ByteArrayOutputStream headerStream)
public static void writeHeader(File input, ByteArrayOutputStream headerStream)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.charset.Charset; public class Main { public static void writeHeader(File input, ByteArrayOutputStream headerStream) { try {/*from ww w .j a v a 2 s .c o m*/ //Read original file contents -> memstream BufferedReader br = new BufferedReader(new FileReader(input)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) sb.append(line).append(String.format(System.lineSeparator())); br.close(); //Header -> original file FileOutputStream outFileStream = new FileOutputStream(input); //header + original file OutputStreamWriter memWriter = new OutputStreamWriter(headerStream, Charset.forName("UTF-8").newEncoder()); memWriter.write(sb.toString()); memWriter.close(); headerStream.writeTo(outFileStream); headerStream.close(); outFileStream.close(); } catch (IOException e) { e.printStackTrace(); } } }