Java OutputStream Write String writeString(OutputStream out, String str)

Here you can find the source of writeString(OutputStream out, String str)

Description

write String

License

Apache License

Declaration

public static void writeString(OutputStream out, String str) throws IOException 

Method Source Code


//package com.java2s;
/* ************************************************************************** *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership./*from w  w w  .  j  av  a 2 s .c  om*/
 *
 * This file is licensed to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * ************************************************************************** */

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void writeString(File file, String str) throws IOException {
        OutputStream output = new FileOutputStream(file);
        writeString(output, str);
    }

    public static void writeString(OutputStream out, String str) throws IOException {
        try {
            byte[] array = str.getBytes("UTF-8");
            ByteArrayInputStream in = new ByteArrayInputStream(array);
            copy(in, out);
        } finally {
            out.close();
        }
    }

    public static void copy(InputStream input, OutputStream output) throws IOException {
        try {
            try {
                byte[] buf = new byte[1024 * 10];
                int len;
                while ((len = input.read(buf)) > 0) {
                    output.write(buf, 0, len);
                }
            } finally {
                output.close();
            }
        } finally {
            input.close();
        }
    }
}

Related

  1. writeString(OutputStream out, String s)
  2. writeString(OutputStream out, String s)
  3. writeString(OutputStream out, String s)
  4. writeString(OutputStream out, String s)
  5. writeString(OutputStream out, String str)
  6. WriteString(OutputStream output, String string)
  7. writeString(OutputStream output, String string)
  8. writeString(String data, int length, OutputStream out)
  9. writeString(String s, OutputStream out)