Java Write String to File writeFile(String fileName, String[] contents)

Here you can find the source of writeFile(String fileName, String[] contents)

Description

description writes the array of strings into the file name speecified

License

Open Source License

Declaration

public static boolean writeFile(String fileName, String[] contents) 

Method Source Code

//package com.java2s;
/* Copyright (C) 2009 SRI International
  */*from  w w  w  .j  a  v  a 2 s .com*/
  * This program 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 2
  * of the License, or (at your option) any later version.
  *
  * This program 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 this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */

import java.io.*;

public class Main {
    /**
     * description writes the array of strings into the file name speecified
     */
    public static boolean writeFile(String fileName, String[] contents) {
        BufferedWriter bufWriter = null;
        String line;
        boolean success = true;

        if (null == contents) {
            return false;
        }
        try {
            bufWriter = new BufferedWriter(new FileWriter(fileName));
            for (int i = 0; i < contents.length; i++) {
                line = contents[i];
                bufWriter.write(line);
            }
        } catch (IOException ioex) {
            success = false;
            System.out.println(
                    "Exception " + ioex.getMessage() + " returned while attempting to write file " + fileName);
        } finally {
            try {
                if (null != bufWriter) {
                    bufWriter.close();
                }
            } catch (IOException ioex2) {
                success = false;
                System.out.println(
                        "Exception " + ioex2.getMessage() + " returned while attempting to close file " + fileName);
            }
        }
        return success;
    }
}

Related

  1. writeFile(String filename, String text)
  2. writeFile(String fileName, String text)
  3. writeFile(String filename, String text, boolean create)
  4. writeFile(String fileName, String text, String characterEncoding)
  5. writeFile(String fileName, String textToSave)
  6. writeFile(String fileName, String[] lines)
  7. writeFile(String filename, StringBuilder sb)
  8. writeFile(String filePath, String content)
  9. writeFile(String filePath, String content)