Java PrintWriter Write saveStrings(String theFilePath, String[] theStrings)

Here you can find the source of saveStrings(String theFilePath, String[] theStrings)

Description

save Strings

License

Open Source License

Declaration

public static void saveStrings(String theFilePath, String[] theStrings) 

Method Source Code

//package com.java2s;
/*//from w ww . j  a va2  s .  com
 * Werkzeug
 *
 * Copyright (C) 2012 Patrick Kochlik + Dennis Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * {@link http://www.gnu.org/licenses/lgpl.html}
 *
 */

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.util.Vector;

public class Main {
    public static void saveStrings(String theFilePath, String[] theStrings) {
        createPath(theFilePath);
        saveStrings(getOutputStream(theFilePath), theStrings);
    }

    public static void saveStrings(OutputStream theOutput, String[] theStrings) {
        try {
            final OutputStreamWriter myOutputStreamWriter = new OutputStreamWriter(theOutput, "UTF-8");
            final PrintWriter myPrintWriter = new PrintWriter(myOutputStreamWriter);
            for (int i = 0; i < theStrings.length; i++) {
                myPrintWriter.println(theStrings[i]);
            }
            myPrintWriter.flush();
            myOutputStreamWriter.close();
            myPrintWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveStrings(OutputStream theOutput, Vector<String> theStrings) {
        try {
            OutputStreamWriter myOutputStreamWriter = new OutputStreamWriter(theOutput, "UTF-8");
            PrintWriter myPrintWriter = new PrintWriter(myOutputStreamWriter);
            for (int i = 0; i < theStrings.size(); i++) {
                myPrintWriter.println(theStrings.get(i));
            }
            myPrintWriter.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveStrings(String theFilePath, Vector<String> theStrings) {
        createPath(theFilePath);
        saveStrings(getOutputStream(theFilePath), theStrings);
    }

    public static void createPath(String filename) {
        final File file = new File(filename);
        final String parent = file.getParent();
        if (parent != null) {
            File unit = new File(parent);
            if (!unit.exists()) {
                unit.mkdirs();
            }
        }
    }

    public static OutputStream getOutputStream(String theFilePath) {
        try {
            final OutputStream myOutputStream = new FileOutputStream(theFilePath);
            return myOutputStream;
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. saveString(String theFilePath, String theString)
  2. saveStringAsFile(String string, String path)
  3. saveStringBufferTofile(String fileName, StringBuffer sb)
  4. saveStrings(final String filename, final String[] sArray)
  5. saveStrings(String filename, String data[])
  6. saveStringToFile(String text, String path)
  7. saveToFile(String buf, String fileName)
  8. saveToFile(String path, String data)