Java Write String to File writeFile(File file, String text)

Here you can find the source of writeFile(File file, String text)

Description

Write to File

License

Open Source License

Parameter

Parameter Description
file a parameter
text a parameter

Return

boolean sucess of writing to file

Declaration

public static boolean writeFile(File file, String text) 

Method Source Code

//package com.java2s;
/*/*from w w w .  ja va 2  s  . co m*/
 * Copyright 2009 Yodlee, Inc.  All Rights Reserved.  Your use of this code 
 * requires a license from Yodlee.  Any such license to this code is 
 * restricted to evaluation/illustrative purposes only. It is not intended 
 * for use in a production environment, and Yodlee disclaims all warranties 
 * and/or support obligations concerning this code, regardless of the terms 
 * of any other agreements between Yodlee and you."
 */

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

public class Main {
    /**
     * Write to File
     * 
     * @param file
     * @param text
     * @return boolean sucess of writing to file
     */
    public static boolean writeFile(File file, String text) {
        return writeFile(file.toString(), text);
    }

    /**
     * Write to File
     * 
     * @param filename
     *            the filename
     * @param text
     *            text to write
     * @reutrn success.
     */
    public static boolean writeFile(String filename, String text) {

        try {
            FileOutputStream os = new FileOutputStream(filename);
            byte textBytes[] = text.getBytes();
            os.write(textBytes, 0, textBytes.length);

        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. writeFile(File file, String data)
  2. writeFile(File file, String data)
  3. writeFile(File file, String data)
  4. writeFile(File file, String data, boolean append)
  5. writeFile(File file, String dataString)
  6. writeFile(File file, String text)
  7. writeFile(File file, String... lines)
  8. writeFile(File file, String... lines)
  9. writeFile(File file, StringBuffer buffer)