Here you can find the source of copyFile(File originalFile, File destinationDir)
public static File copyFile(File originalFile, File destinationDir) throws IOException
//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 File copyFile(File originalFile, File destinationDir) throws IOException { File[] fileList = destinationDir.listFiles(); String nextFilename;// w w w .j av a2s. co m if (fileList.length > 0) { String lastFilename = destinationDir.listFiles()[fileList.length - 1].getName(); int idxDotLastFile = lastFilename.indexOf("."); String originalFileName = originalFile.getName(); int idxDotOriginalFile = originalFileName.indexOf("."); String fileSuffix = originalFileName.substring(idxDotOriginalFile + 1); int idLastFilename = Integer.parseInt(lastFilename.substring(0, idxDotLastFile)); nextFilename = String.format("%09d.%s", (idLastFilename + 1), fileSuffix); } else { String lastFilename = originalFile.getName(); int idxPunkt = lastFilename.indexOf("."); nextFilename = String.format("%09d", 1) + lastFilename.substring(idxPunkt); } FileInputStream in = new FileInputStream(originalFile); FileOutputStream out = new FileOutputStream(new File(destinationDir, nextFilename)); while (true) { int data = in.read(); if (data == -1) break; out.write(data); } in.close(); out.close(); return new File(destinationDir, nextFilename); } }