Java FileOutputStream Write saveToFile(String fileName, String fileCode, File dir, String fileExtention)

Here you can find the source of saveToFile(String fileName, String fileCode, File dir, String fileExtention)

Description

save To File

License

Open Source License

Declaration

public static File saveToFile(String fileName, String fileCode, File dir, String fileExtention) 

Method Source Code


//package com.java2s;
/*//from  w w w . j a  v a2s.  c  o m
 * (c) Copyright 2010-2011 AgileBirds
 *
 * This file is part of OpenFlexo.
 *
 * OpenFlexo 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.
 *
 * OpenFlexo 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 OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static final String OUTPUT_FILES_ENCODING = "UTF-8";

    public static File saveToFile(String fileName, String fileCode, File dir, String fileExtention) {
        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        FileOutputStream fos = null;
        try {
            File dest = new File(dir.getAbsolutePath() + File.separator + getFileName(fileName, fileExtention));
            dest.createNewFile();
            byte[] b;
            if (fileCode != null)
                b = fileCode.getBytes(OUTPUT_FILES_ENCODING);
            else
                b = new byte[0];
            fos = new FileOutputStream(dest);
            fos.write(b);
            return dest;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (fos != null)
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

    public static String getFileName(String fileName, String fileExtention) {
        if (fileExtention != null)
            return fileName + "." + fileExtention;
        else
            return fileName;
    }
}

Related

  1. saveToFile(InputStream in, String fileName)
  2. saveToFile(InputStream stream, File file)
  3. saveToFile(String baseName, String extension, byte[] content)
  4. saveToFile(String data, File file)
  5. saveToFile(String data, File file)
  6. saveToFile(String filePath, InputStream in)
  7. saveToTextFile(String filename, byte[] text)
  8. SaveToZipSB(File file, String ZippedFile, StringBuilder sb)
  9. saveZip(String fileName, Map dataMap)