Java BufferedWriter Write writeFileContent(File audioFile, short[] data, boolean littleEndian)

Here you can find the source of writeFileContent(File audioFile, short[] data, boolean littleEndian)

Description

write short array as byte file.

License

Open Source License

Parameter

Parameter Description
audioFile a parameter

Declaration

public static void writeFileContent(File audioFile, short[] data, boolean littleEndian) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.util.*;

public class Main {
    public final static String STD_ENCODING = "ISO8859-1";

    /**//w  w w .j  a  va 2  s .c  o m
     * Write some String to a file.
     * 
     * @param content
     *            The String.
     * @param fileName
     *            Description of the Parameter
     * @exception Exception
     *                Description of the Exception
     */
    public static void writeFileContent(String fileName, String content) throws Exception {
        writeFileContent(fileName, content, STD_ENCODING);
    }

    /**
     * Write a String (might contain newlines) to a file.
     * 
     * @param fileName
     *            The file name.
     * @param content
     *            The String to write.
     * @param charEncoding
     *            The char encoding, e.g. Constants.CHAR_ENCODING.
     * @throws Exception
     *             E.g. if the file is not writable.
     */
    public static void writeFileContent(String fileName, String content, String charEncoding) throws Exception {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), charEncoding));
            bw.write(content);
        } catch (Exception e) {
            System.err.println("error trying to get file content" + e.toString());
            // e.printStackTrace();
        } finally {
            bw.close();
            bw = null;
        }
    }

    /**
     * Write a Vector of String (might contain newlines) to a file.
     * 
     * @param fileName
     *            The file name.
     * @param content
     *            The Vector of Strings to write.
     * @param charEncoding
     *            The char encoding, e.g. "UTF-8".
     * @throws Exception
     *             E.g. if the file is not writable.
     */
    public static void writeFileContent(String fileName, Vector<String> content, String charEncoding)
            throws Exception {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), charEncoding));
            for (Iterator<?> iterator = content.iterator(); iterator.hasNext();) {
                String line = (String) iterator.next();
                bw.write(line + "\n");
            }
        } catch (Exception e) {
            System.err.println("error trying to get file content" + e.toString());
            e.printStackTrace();
        } finally {
            bw.close();
            bw = null;
        }
    }

    /**
     * Write some Strings to a file.
     * 
     * @param content
     *            The String.
     * @param fileName
     *            Description of the Parameter
     * @exception Exception
     *                Description of the Exception
     */
    public static void writeFileContent(String fileName, Vector<String> content) throws Exception {
        writeFileContent(fileName, content, STD_ENCODING);
    }

    /**
     * writes a byte array to file
     * 
     * @param fileName
     *            the filename
     * @param content
     *            the byte array
     * @exception Exception
     */
    public static void writeFileContent(String fileName, byte[] content) throws Exception {
        BufferedOutputStream writer = null;
        try {
            writer = new BufferedOutputStream(new FileOutputStream(fileName));
            writer.write(content);
        } catch (Exception e) {
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * Returns the content of a given file as String.
     * 
     * @param f
     *            Description of the Parameter
     * @param content
     *            Description of the Parameter
     * @exception Exception
     *                Description of the Exception
     */
    public static void writeFileContent(File f, String content) throws Exception {
        writeFileContent(f.getAbsolutePath(), content);
    }

    public static void writeFileContent(File f, String content, String charEncoding) throws Exception {
        writeFileContent(f.getAbsolutePath(), content, charEncoding);
    }

    /**
     * write short array as byte file.
     * 
     * @param audioFile
     */
    public static void writeFileContent(File audioFile, short[] data, boolean littleEndian) {
        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(audioFile));
            byte hb, lb;
            if (littleEndian) {
                for (int i = 0; i < data.length; i++) {
                    lb = (byte) data[i];
                    hb = (byte) (data[i] >> 8);
                    out.write(lb);
                    out.write(hb);
                }
            } else {
                // big Endian
                for (int i = 0; i < data.length; i++)
                    out.writeShort(data[i]);
            }
            out.close();
        } catch (Exception audioEx) {
            System.err.println("problem writing audio-track: " + audioEx);
        }
    }
}

Related

  1. writeFile(String line, File file)
  2. writefile(String m, String path)
  3. writeFile(String outputLocation, String fileName, List content)
  4. writeFile(String p_name, String p_encoding, String p_content)
  5. writeFileAppend(String filePath, String content)
  6. writeFileContents(File file, String contents)
  7. writeFilenames(String folderPath, LinkedList listOfPopulationFiles)
  8. writeFileText(File file, String text)
  9. writeFileToString(String filePath, String fileContent, String encoding, boolean append)