write String to FileOutputStream - Java java.io

Java examples for java.io:OutputStream

Description

write String to FileOutputStream

Demo Code


import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main{
    public static void writeString(FileOutputStream bos, String s,
            int length) throws IOException {

        int count = 0;

        if (s == null) {

            count = length;//  www .j a va2 s .c  o m
        } else {
            bos.write(s.getBytes());
            count = length - s.getBytes().length;
        }

        if (count > 0) {
            byte[] xx = new byte[count];

            for (int i = 0; i < count; i++) {
                xx[i] = 0;
            }

            bos.write(xx);
        }
    }
}

Related Tutorials