Android FileOutputStream Write WriteString(FileOutputStream stream, String text)

Here you can find the source of WriteString(FileOutputStream stream, String text)

Description

Write String

License

Open Source License

Declaration

public static void WriteString(FileOutputStream stream, String text)
            throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2010-2011 Dmitriy Nesterov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version./*from   w w  w.  j  av  a2s .c o  m*/
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void WriteString(FileOutputStream stream, String text)
            throws IOException {
        byte[] bytes = text.getBytes();
        WriteInt(stream, bytes.length);
        stream.write(bytes);
    }

    public static void WriteInt(FileOutputStream stream, int value)
            throws IOException {
        stream.write(IntToBytes(value));
    }

    public static byte[] IntToBytes(int value) {
        byte[] result = new byte[4];
        result[0] = (byte) (value >> 24);
        result[1] = (byte) ((value << 8) >> 24);
        result[2] = (byte) ((value << 16) >> 24);
        result[3] = (byte) ((value << 24) >> 24);

        return result;
    }
}

Related

  1. WriteArrayOfStringPairs(FileOutputStream stream, ArrayList> pairs)
  2. WriteInt(FileOutputStream stream, int value)
  3. saveFileContent(String file, String content, String encode)
  4. getXMLAsString(File xmlFile, String fileName)
  5. Write(String fileName, String message)