Here you can find the source of copyToFolderAs(File fromFile, File folder, String asName)
Parameter | Description |
---|---|
fromFile | a parameter |
toFile | a parameter |
public static File copyToFolderAs(File fromFile, File folder, String asName)
//package com.java2s; /*/*from ww w.j a v a2 s.c om*/ * #Copyright (C) Peeyush Sahu <peeyush215[at]gmail.com> * # * #This program is free software: you can redistribute it and/or modify * #it under the terms of the GNU General Public License as published by * #the Free Software Foundation, either version 3 of the License, or * #(at your option) any later version. * # * #This program is distributed in the hope that it will be useful, * #but WITHOUT ANY WARRANTY; without even the implied warranty of * #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * #GNU General Public License for more details. * # * #You should have received a copy of the GNU General Public License * #along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.*; import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; public class Main { private static final Logger mylogger = Logger.getLogger("com.lia.core"); /** * * @param fromFile * @param toFile * @return */ public static File copyToFolderAs(File fromFile, File folder, String asName) { mylogger.log(Level.INFO, "Copying:[{0}] file to folder [{1}] as {2}", new Object[] { fromFile.getAbsolutePath(), folder.getAbsolutePath(), asName }); if (!folder.isDirectory() || !folder.exists()) { mylogger.log(Level.SEVERE, "Invalid folder {0} ! COPY FAILED", new Object[] { folder.getAbsolutePath() }); return null; } if (!fromFile.exists()) { mylogger.log(Level.SEVERE, "File {0} is doesn't exists! COPY FAILED", new Object[] { fromFile.getAbsolutePath() }); return null; } else { try { File newFile = new File(folder, asName); if (!newFile.createNewFile()) { mylogger.log(Level.SEVERE, "File {0} creation failed!", new Object[] { newFile.getAbsolutePath() }); return null; } else { copy12(fromFile, newFile); mylogger.info("Copy completed"); return newFile; } } catch (IOException ex) { mylogger.log(Level.SEVERE, "Something went wrong; error while copying: ", ex); } } return null; } private static void copy12(File file1, File file2) { try { mylogger.log(Level.INFO, "Copy12:[0}] -> [{1}]{ ...", new Object[] { file1.getAbsolutePath(), file2.getAbsolutePath() }); FileChannel in = (new FileInputStream(file1)).getChannel(); FileChannel out = (new FileOutputStream(file2)).getChannel(); in.transferTo(0, file1.length(), out); in.close(); out.close(); mylogger.info("... done}"); } catch (Exception ex) { mylogger.log(Level.SEVERE, "Error while copying: ", ex); } } }