Java Write String to File writeFile(String fileName, String fileContent)

Here you can find the source of writeFile(String fileName, String fileContent)

Description

Writes the content of the string fileContent to a file.

License

Open Source License

Parameter

Parameter Description
fileName the file name
fileContent the file content

Exception

Parameter Description
Exception the exception

Declaration

public static void writeFile(String fileName, String fileContent) throws Exception 

Method Source Code

//package com.java2s;
/*//from  www  .  j  av  a2  s  . co  m
 * (c) 2012 Michael Schwartz e.U.
 * All Rights Reserved.
 * 
 * This program is not a free software. The owner of the copyright
 * can license the software for you. You may not use this file except in
 * compliance with the License. In case of questions please
 * do not hesitate to contact us at idx@mschwartz.eu.
 * 
 * Filename: FileUtil.java
 * Created: 30.05.2012
 * 
 * Author: $LastChangedBy$
 * Date: $LastChangedDate$
 * Revision: $LastChangedRevision$
 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.FileWriter;

public class Main {
    /**
     * The Constant maxBlockSize is the maximum size of one block which will be
     * allocated to copy a file.
     */
    public static final int maxBlockSize = 10 * 1024 * 1024;

    /**
     * Writes the content of the string <code>fileContent</code> to a file.
     * 
     * @param fileName
     *            the file name
     * @param fileContent
     *            the file content
     * @throws Exception
     *             the exception
     */
    public static void writeFile(String fileName, String fileContent) throws Exception {
        File file = new File(fileName);
        FileWriter writer = new FileWriter(file);
        writer.write(fileContent);
        writer.close();
    }

    /**
     * Writes the content of
     * <code>content</content> to a file denoted by <code>fileName</code>. The
     * file will be created with the extension ".filepart", the content will be
     * stored into the file and then the file will be renamed to the desired
     * name. If a file with the same name is already existing that file will be
     * deleted.
     * 
     * @param fileName
     *            the file name
     * @param content
     *            the content
     * @throws Exception
     *             the exception
     */
    public static void writeFile(String fileName, byte[] content) throws Exception {
        FileOutputStream fos = new FileOutputStream(fileName + ".filepart");
        int done = 0;
        while (content.length - done > 0) {
            if (content.length - done > maxBlockSize) {
                fos.write(content, done, maxBlockSize);
                done += maxBlockSize;
            } else {
                fos.write(content, done, content.length - done);
                done += content.length;
            }
        }
        fos.close();
        boolean ok = new File(fileName).delete();
        File file = new File(fileName + ".filepart");
        ok = file.renameTo(new File(fileName));
        if (!ok) {
            throw new RuntimeException("FileSaveFailed");
        }
    }
}

Related

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