Java File Copy copyFileToPath(String fileToPath, String fileName, String sourceFile)

Here you can find the source of copyFileToPath(String fileToPath, String fileName, String sourceFile)

Description

copy File To Path

License

LGPL

Declaration

public static boolean copyFileToPath(String fileToPath,
        String fileName, String sourceFile) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JDialog;

import javax.swing.JOptionPane;

import javax.swing.UIManager;

public class Main {

    public static boolean copyFileToPath(String fileToPath,
            String fileName, InputStream is) {
        try {/*from  w  w w .  j  a v a  2  s .c o m*/
            File dic = new File(fileToPath);
            if (!dic.exists()) {
                dic.mkdirs();
            }
            OutputStream os = new BufferedOutputStream(
                    new FileOutputStream(fileToPath + "/" + fileName));
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            os.flush();
            is.close();
            os.close();
            return true;
        } catch (Exception e) {
            return showExceptionMessage(e);
        }
    }

    public static boolean copyFileToPath(String fileToPath,
            String fileName, String sourceFile) {
        try {
            File dic = new File(fileToPath);
            if (!dic.exists()) {
                dic.mkdirs();
            }
            InputStream is = new BufferedInputStream(new FileInputStream(
                    sourceFile));
            OutputStream os = new BufferedOutputStream(
                    new FileOutputStream(fileToPath + "/" + fileName));
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                os.write(bytes, 0, len);
            }
            os.flush();
            is.close();
            os.close();
            return true;
        } catch (Exception e) {
            return showExceptionMessage(e);
        }
    }

    public static boolean showExceptionMessage(Exception e) {
        try {
            UIManager.setLookAndFeel(UIManager
                    .getSystemLookAndFeelClassName());
        } catch (Exception el) {
            JOptionPane.showMessageDialog(new JDialog(), e.getMessage());
            el.printStackTrace();
        }
        JOptionPane.showMessageDialog(new JDialog(), e.getMessage());
        e.printStackTrace();
        return false;
    }
}

Related

  1. copyFiles(File srcDir, File destDir, String... regexps)
  2. copyFiles(File srcFile, File destFolder)
  3. copyFiles(InputStream inStream, FileOutputStream outStream)
  4. copyFiles(String from, String to)
  5. copyFiles(String fromDirName, String toDirName)
  6. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  7. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  8. fileCopy(File source, String dest)
  9. fileCopy(File srcFile, File tarFile)