Java Byte Array Save to File nio writeHeader(File input, ByteArrayOutputStream headerStream)

Here you can find the source of writeHeader(File input, ByteArrayOutputStream headerStream)

Description

write Header

License

Open Source License

Declaration

public static void writeHeader(File input, ByteArrayOutputStream headerStream) 

Method Source Code


//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();
        }
    }
}

Related

  1. fileWriteBytes(byte[] data, String file)
  2. writeBytes(final byte[] bytes, final OutputStream output)
  3. writeIntChars(int value, int index, byte[] buf)
  4. writeToBinaryFile(String filename, byte[] data)
  5. writeToFile(File file, byte[] content)