Java Write String to File writeFile(String FileName, String data)

Here you can find the source of writeFile(String FileName, String data)

Description

{ method

License

Apache License

Parameter

Parameter Description
FileName name of file to create
data date to write

Return

true = success }

Declaration

public static boolean writeFile(String FileName, String data) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**/* w  w  w.  j  a  v  a2s  . c o m*/
     * { method
     *
     * @param FileName name of file to create
     * @param data     date to write
     * @return true = success
     *         }
     * @name writeFile
     * @function write the string data to the file Filename
     */
    public static boolean writeFile(String FileName, String data) {
        try {
            PrintWriter out = openPrintWriter(FileName);
            if (out != null) {
                out.print(data);
                out.close();
                return (true);
            }
            return (false);
            // failure
        } catch (SecurityException ex) {
            return (false); // browser disallows
        }
    }

    /**
     * { method
     *
     * @param FileName name of file to create
     * @param data     date to write
     * @return true = success
     *         }
     * @name writeFile
     * @function write the string data to the file Filename
     */
    public static boolean writeFile(File TheFile, String data) {
        PrintWriter out = null;
        try {
            out = openPrintWriter(TheFile);
            if (out != null) {
                out.print(data);
                out.close();
                return (true);
            }
            return (false);
            // failure
        } catch (SecurityException ex) {
            return (false); // browser disallows
        } finally {
            if (out != null)
                out.close();
        }
    }

    /**
     * { method
     *
     * @param name name of file
     * @return the stream - null for failure
     *         }
     * @name openPrintWriter
     * @function open a PrintWriter to a file with name
     */
    public static PrintWriter openPrintWriter(String name) {
        FileOutputStream file = null;
        try {
            file = new FileOutputStream(name);
            return new PrintWriter(new BufferedOutputStream(file));
        } catch (SecurityException ee) {
            return (null);
        } catch (IOException ee) {
            return (null);
        }
    }

    /**
     * { method
     *
     * @param name name of file
     * @return the stream - null for failure
     *         }
     * @name openPrintWriter
     * @function open a PrintWriter to a file with name
     */
    public static PrintWriter openPrintWriter(File name) {
        FileOutputStream file = null;
        try {
            file = new FileOutputStream(name);
            return new PrintWriter(new BufferedOutputStream(file));
        } catch (SecurityException ee) {
            return (null);
        } catch (IOException ee) {
            return (null);
        }
    }
}

Related

  1. writeFile(String fileName, String contents)
  2. writeFile(String fileName, String contents)
  3. writeFile(String fileName, String contentStr, String charset)
  4. writeFile(String filename, String data)
  5. writeFile(String fileName, String data)
  6. writeFile(String fileName, String encoding, String content)
  7. writeFile(String fileName, String fileContent)
  8. writeFile(String fileName, String fileData)
  9. writeFile(String fileName, String message)