Copy a file with FileInputStream and FileOutputStream in Java

Description

The following code shows how to copy a file with FileInputStream and FileOutputStream.

Example


/* w w  w . jav a  2s . co  m*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String args[]) throws IOException {
    int i;
    FileInputStream fin;
    FileOutputStream fout;

    fin = new FileInputStream(args[0]);
    fout = new FileOutputStream(args[1]);
    do {
      i = fin.read();
      if (i != -1)
        fout.write(i);
    } while (i != -1);

    fin.close();
    fout.close();
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip