Java FileOutputStream Write saveFile(File f, String fileName, File desDir)

Here you can find the source of saveFile(File f, String fileName, File desDir)

Description

saveFile

License

Open Source License

Parameter

Parameter Description
f f
fileName fileName
desDir desDir

Exception

Parameter Description
IOException IOException

Declaration

public static void saveFile(File f, String fileName, File desDir) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w  ww .  jav  a 2  s. c  om
 * Copyright (C) 2010 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**
     * .
     */
    public static final int BUFFER_SIZE = 1024 * 8;

    /**
     * saveFile
     *
     * @param f f
     * @param fileName fileName
     * @param desDir desDir
     * @throws IOException IOException
     */
    public static void saveFile(File f, String fileName, File desDir) throws IOException {
        InputStream stream = new FileInputStream(f);
        OutputStream out = new FileOutputStream(desDir.getAbsolutePath() + File.separatorChar + fileName);
        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        stream.close();
        out.close();
    }

    /**
     * Save file
     *
     * @param f f
     * @param fileName fileName
     * @param desDir desDir
     * @param override override
     * @throws IOException IOException
     */
    public static void saveFile(File f, String fileName, File desDir, boolean override) throws IOException {
        File file = new File(desDir.getAbsolutePath() + File.separatorChar + fileName);
        if (override) {
            if (file.exists()) {
                file.delete();
            }
        }
        InputStream stream = new FileInputStream(f);
        OutputStream out = new FileOutputStream(file);
        int bytesRead;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        stream.close();
        out.close();
    }

    /**
     * Deletes a file or a directory.
     *
     * @param file the file or directory which should be deleted.
     * @return true when the file could be deleted
     */
    public static boolean delete(File file) {
        if (file.isDirectory()) {
            String[] children = file.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = delete(new File(file, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // The directory is now empty so delete it
        return file.delete();
    }
}

Related

  1. saveDataToFile(String filePath, byte[] data)
  2. saveDownloadedFile(InputStream is, String destination)
  3. saveEntry(String destPath, ZipEntry target, ZipFile zf)
  4. saveFile(byte[] fileData, String outputDir, String fileName)
  5. saveFile(File f, InputStream stream)
  6. saveFile(File f, String fileName, File desDir)
  7. saveFile(File file, byte[] contenido)
  8. saveFile(File file, InputStream is)
  9. saveFile(File file, String fileName, String filesDirectory)