Java Text File Write writeStringToFile(String string, String fileName)

Here you can find the source of writeStringToFile(String string, String fileName)

Description

write String To File

License

Open Source License

Declaration

public static void writeStringToFile(String string, String fileName) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  w w .  j  a v a  2 s  .  c o  m*/
 * SwingTech Software - http://cooksarm.sourceforge.net/
 *
 * Copyright (C) 2011 Joe Rice
 * All rights reserved.
 * 
 * SwingTech Cooks Arm is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * SwingTech Cooks Arm is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SwingTech Cooks Arm; If not, see <http://www.gnu.org/licenses/>. 
 * 
 */

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import java.io.Writer;

public class Main {
    public static void writeStringToFile(String string, String fileName) throws IOException {
        File file = null;
        boolean fileCreated = false;

        if (fileName == null || string == null) {
            throw new IllegalArgumentException(
                    "fileName or string passed in was NULL.  Must have both these values.");
        }

        file = new File(fileName);

        if (!file.exists()) {
            fileCreated = file.createNewFile();
            if (!fileCreated) {
                throw new RuntimeException("Could not create file of name:  " + fileName);
            }
        }
        //use buffering
        Writer output = new BufferedWriter(new FileWriter(file));
        try {
            //FileWriter always assumes default encoding is OK!
            output.write(string);
        } finally {
            output.close();
        }
    }
}

Related

  1. writeStringToFile(String string, File dstFile)
  2. writeStringToFile(String string, File file)
  3. writeStringToFile(String string, File file)
  4. writeStringToFile(String string, File file)
  5. writeStringToFile(String string, File file)
  6. writeStringToFile(String string, String fileName, boolean append)
  7. writeStringToFile(String string, String path)
  8. writeStringToFile(String stringContent, String fileName)
  9. writeStringToFile(String stringToBeWritten, String filePath)