Java Utililty Methods FileInputStream Copy

List of utility methods to do FileInputStream Copy

Description

The list of methods to do FileInputStream Copy are organized into topic(s).

Method

voidcopyFile(File from, File to)
copy File
try {
    Scanner fromScanner = new Scanner(new FileInputStream(from));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(to)));
    while (fromScanner.hasNextByte()) {
        out.print(fromScanner.nextByte());
    out.close();
    fromScanner.close();
...
voidcopyFile(File from, File to)
Copy a file from one location to another.
if (!from.exists()) {
    throw new FileNotFoundException("File '" + from.getAbsolutePath() + "' does not exist.");
InputStream in = null;
try {
    in = new FileInputStream(from);
    writeFile(in, to);
} finally {
...
voidcopyFile(File from, File to)
Copy a file from one location to another.
try (InputStream in = new FileInputStream(from); OutputStream out = new FileOutputStream(to)) {
    byte[] buff = new byte[1024];
    int len;
    while ((len = in.read(buff)) > 0) {
        out.write(buff, 0, len);
booleancopyFile(File from, File to, byte[] buf)
copy File
if (buf == null)
    buf = new byte[BUFFER_SIZE];
FileInputStream from_s = null;
FileOutputStream to_s = null;
try {
    from_s = new FileInputStream(from);
    to_s = new FileOutputStream(to);
    for (int bytesRead = from_s.read(buf); bytesRead > 0; bytesRead = from_s.read(buf)) {
...
voidcopyFile(File from, File toDir)
copy File
if (!toDir.isDirectory() || !from.exists()) {
    throw new IOException("Destination is no Directory or source not exists.");
copy(from, new File(toDir, from.getName()));
voidcopyFile(File from, File toFile)
copy File
copyFile(from, toFile, null);
voidcopyFile(File fromFile, File toFile)
Legacy method to copy a file from one location to another
FileOutputStream to;
try (FileInputStream from = new FileInputStream(fromFile)) {
    to = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = from.read(buffer)) > 0)
        to.write(buffer, 0, bytesRead);
to.close();
booleancopyFile(File fromFile, File toFile)
Copy a file
if (toFile.isDirectory())
    toFile = new File(toFile, fromFile.getName());
FileInputStream from = null;
FileOutputStream to = null;
try {
    from = new FileInputStream(fromFile);
    File p = toFile.getParentFile();
    if (p != null && !p.exists())
...
voidcopyFile(File fromFile, File toFile)
copy File
String fromFileName = fromFile.getName();
String toFileName = toFile.getName();
if (!fromFile.exists()) {
    throw new IOException("FileCopy: " + "no such source file: " + fromFile.getPath());
if (!fromFile.isFile()) {
    throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName);
if (!fromFile.canRead()) {
    throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName);
if (toFile.isDirectory()) {
    toFile = new File(toFile, fromFile.getName());
String parent = toFile.getParent();
if (parent == null) {
    parent = System.getProperty("user.dir");
File dir = new File(parent);
if (!dir.exists()) {
    throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent);
if (dir.isFile()) {
    throw new IOException("FileCopy: " + "destination is not a directory: " + parent);
if (!dir.canWrite()) {
    throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent);
FileInputStream from;
from = new FileInputStream(fromFile);
copyFile(from, toFile);
voidcopyFile(File fromFile, File toFile, IProgressMonitor monitor)
Copies a file, given the file to be copied and copy to
byte[] data = new byte[4096];
InputStream in = new FileInputStream(fromFile);
toFile.delete();
OutputStream out = new FileOutputStream(toFile);
monitor.beginTask("Copy " + fromFile.toString(), (int) fromFile.length());
int count = in.read(data);
while (count != -1) {
    out.write(data, 0, count);
...