Java FileOutputStream Write saveFile(String dir, String fileName, String data)

Here you can find the source of saveFile(String dir, String fileName, String data)

Description

save File

License

Apache License

Declaration

public static boolean saveFile(String dir, String fileName, String data) 

Method Source Code

//package com.java2s;
/*//  w  w w.  j  ava2  s . c o m
 * Copyright (C) 2017, Megatron King
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;

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

public class Main {
    public static boolean saveFile(String dir, String fileName, String data) {
        return saveFile(dir, fileName, data, false, false);
    }

    private static boolean saveFile(String dir, String fileName,
            String data, boolean mkdir, boolean append) {
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            if (!mkdir) {
                return false;
            }
            createFolders(dirFile);
        }

        File dest = new File(dir, fileName);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(dest, append);
            fos.write(data.getBytes());
            fos.flush();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean createFolders(File folder) {
        return folder.mkdirs();
    }
}

Related

  1. savefile(InputStream source, String destination)
  2. saveFile(InputStream uploadedInputStream, String serverLocation)
  3. saveFile(String content, String path)
  4. saveFile(String desEncryptTempFile, byte[] text)
  5. saveFile(String destinationFile, String contents)
  6. saveFile(String file, String text)
  7. saveFile(String fileName, byte[] bytes)
  8. saveFile(String filename, byte[] content)
  9. saveFile(String fileName, byte[] data)