Java Text File Write writeString(File file, String str)

Here you can find the source of writeString(File file, String str)

Description

write String

License

Apache License

Declaration

public static void writeString(File file, 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  a  v a  2 s.  com
 *
 * 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(File file, String content, boolean append)
  2. writeString(File file, String data)
  3. writeString(File file, String s, int bufferSize, String encoding)
  4. writeString(File file, String str, String encoding)
  5. writeString(File file, String string)
  6. writeString(File file, String string)
  7. writeString(File outputFile, String text)