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

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

Description

Writes a string (generated from running the previous method) on a plain text file.

License

Open Source License

Parameter

Parameter Description
stringToWrite The string used to generate the text file.
fileName Name it whatever you want.

Declaration

public static File writeStringToFile(String stringToWrite, String fileName) 

Method Source Code


//package com.java2s;
/*//w  ww  . ja  v a 2 s . c  o m
 * 2010, http://github.com/pauloewerton/partSOM4Grid
 * This file is part of partSOM4Grid 
 *
 * partSOM4Grid is free software: you can redistribute it and/or modify it under the
 * terms of the Artistic License 2.0 as published by the OSI.
 * 
 * This program is distributed in 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 Artistic License 2.0
 * for more details. 
 * 
 * You should have received a copy of the Artistic License 2.0
 * along with this program. See <www.opensource.org/licenses/artistic-license-2.0>.
 * 
 */

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

public class Main {
    /**
     * Writes a string (generated from running the previous method) on a plain
     * text file.
     * @param stringToWrite The string used to generate the text file.
     * @param fileName Name it whatever you want.
    **/
    public static File writeStringToFile(String stringToWrite, String fileName) {

        File file = new File(fileName);
        FileWriter writer = null;

        try {

            writer = new FileWriter(file);
            writer.write(stringToWrite);
            writer.close();

        } catch (IOException ex) {

            ex.printStackTrace();
        }

        return file;
    }
}

Related

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