Java Write String to File writeFile(File file, StringBuffer buffer)

Here you can find the source of writeFile(File file, StringBuffer buffer)

Description

write File

License

Open Source License

Declaration

public static void writeFile(File file, StringBuffer buffer) throws IOException 

Method Source Code

//package com.java2s;
/***************************************************************************
 * Copyright (c) 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from  w w  w  . jav  a  2s.  c  o  m*/
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/

import java.io.File;

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

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    public static void writeFile(File file, StringBuffer buffer) throws IOException {
        writeFile(file, buffer.toString());
    }

    public static void writeFile(File file, String str) throws IOException {
        writeFile(file, str.getBytes());
    }

    public static void writeFile(File file, byte[] bytes) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            os.write(bytes);
        } finally {
            close(os);
        }

    }

    /**
     * No exception <code>InputStream</code> close method.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>OutputStream</code> close method.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Reader</code> close method.
     */
    public static void close(Reader rd) {
        if (rd != null) {
            try {
                rd.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }

    /**
     * No exception <code>java.io.Writer</code> close method.
     */
    public static void close(Writer wr) {
        if (wr != null) {
            try {
                wr.close();
            } catch (Exception ignore) {
                ignore.printStackTrace();
            }
        }
    }
}

Related

  1. writeFile(File file, String dataString)
  2. writeFile(File file, String text)
  3. writeFile(File file, String text)
  4. writeFile(File file, String... lines)
  5. writeFile(File file, String... lines)
  6. writeFile(File filename, ArrayList lines)
  7. writeFile(final File file, final String content)
  8. writeFile(final File file, final String contents)
  9. writeFile(final File file, final String contents)