Java File Write via ByteBuffer writeToFile(String path, ByteOrder byteOrder)

Here you can find the source of writeToFile(String path, ByteOrder byteOrder)

Description

write To File

License

Apache License

Declaration

public static void writeToFile(String path, ByteOrder byteOrder) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    public static void writeToFile(String path, ByteOrder byteOrder) {
        String fileName = "My new STL";

        File file = new File(path);
        DataOutputStream output = null;

        try {/*from w w  w.j a va  2s.c  o m*/
            output = new DataOutputStream(new FileOutputStream(path));

            byte[] fileNameByteArray = fileName.getBytes();

            ByteBuffer buffer = ByteBuffer.allocate(500);
            buffer.order(byteOrder);

            byte[] fillerArray = new byte[80];
            System.arraycopy(fileNameByteArray, 0, fillerArray, 0, fileNameByteArray.length);

            buffer.put(fillerArray);
            buffer.putInt(1);

            //Normal
            buffer.putFloat(0.0f);
            buffer.putFloat(-100.0f);
            buffer.putFloat(0.0f);

            //P1
            buffer.putFloat(0.0f);
            buffer.putFloat(0.0f);
            buffer.putFloat(10.0f);
            //P2
            buffer.putFloat(10.0f);
            buffer.putFloat(0.0f);
            buffer.putFloat(0.0f);
            //P3
            buffer.putFloat(0.0f);
            buffer.putFloat(0.0f);
            buffer.putFloat(0.0f);

            buffer.putShort(((short) 0));

            byte[] bytes = buffer.array();
            output.write(bytes);

            output.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeShort(int i, DataOutput out)
  2. writeString(ByteArrayOutputStream bout, String val)
  3. writeStringContents(String contents, WritableByteChannel channel)
  4. writeStringData(DataOutputStream mDataOutputStream, String token)
  5. writeStringToSocketChannel(String text, SocketChannel sc)
  6. writeUtf8(DataOutput out, String string)
  7. writeVector(DataOutputStream output, float[] vector)
  8. writeWaveFile44100_16Bit_Mono(short[] data, File file)