Java FileOutputStream Write saveFile(File file, String fileName, String filesDirectory)

Here you can find the source of saveFile(File file, String fileName, String filesDirectory)

Description

save File

License

Open Source License

Declaration

public static void saveFile(File file, String fileName, String filesDirectory) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void saveFile(File file, String fileName, String filesDirectory) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;

        File dir = new File(filesDirectory);
        if (!dir.exists())
            dir.mkdirs();//from  w  w w.  j av a2 s .  c  o m

        String targetPath = dir.getPath() + File.separator + fileName;
        System.out.println("source file path ::" + file.getAbsolutePath());
        System.out.println("saving file to ::" + targetPath);
        File destinationFile = new File(targetPath);
        try {
            in = new FileInputStream(file);
            out = new FileOutputStream(destinationFile);
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }

        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }

    }
}

Related

  1. saveFile(File f, InputStream stream)
  2. saveFile(File f, String fileName, File desDir)
  3. saveFile(File f, String fileName, File desDir)
  4. saveFile(File file, byte[] contenido)
  5. saveFile(File file, InputStream is)
  6. saveFile(File file, String savePath)
  7. saveFile(final byte[] bytes, final File target)
  8. saveFile(final File file, final byte[] dataToSave)
  9. saveFile(final File file, final String contents, final String encoding)