Java FileInputStream Copy copyFile(File originalFile, File destinationDir)

Here you can find the source of copyFile(File originalFile, File destinationDir)

Description

copy File

License

Open Source License

Declaration

public static File copyFile(File originalFile, File destinationDir) 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 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);
    }
}

Related

  1. copyFile(File orig, File dest)
  2. copyFile(File orig, File dest, boolean overwrite)
  3. copyFile(File original, File copy)
  4. copyFile(File original, File destination)
  5. copyFile(File original, File parent)
  6. copyFile(File pSrc, File pDst, boolean pForce)
  7. copyFile(File source, File dest)
  8. copyFile(File source, File dest)
  9. copyFile(File source, File dest)