FileInputStream and FileOutputStream to copy a file


  import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {

    FileInputStream fin = null;
    FileOutputStream fout = null;

    File file = new File("C:/myfile1.txt");

    fin = new FileInputStream(file);
    fout = new FileOutputStream("C:/myfile2.txt");

    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = fin.read(buffer)) > 0) {
      fout.write(buffer, 0, bytesRead);
    }
    fin.close();
    fout.close();
  }
}
  
Home 
  Java Book 
    File Stream  

FileInputStream:
  1. FileInputStream
  2. Skip n bytes while reading the file using FileInputStream
  3. FileInputStream and FileOutputStream to copy a file