Here you can find the source of copyFileToPath(String fileToPath, String fileName, String sourceFile)
public static boolean copyFileToPath(String fileToPath, String fileName, String sourceFile)
//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; } }