How to copy file in Java using FileInputStream and FileOutputStream?
UPDATED: 01 July 2015
Tags:
FileInputStream
,
FileOutputStream
This excerpt shows how you can copy content of source file to destination file using java.io.FileInputStream and java.io.FileOutputStream.
Source Code (Java 1.6 or less)
Source Code (Java 1.7 or above)(try-with-resources
Other References:
What is try-with-resources in Java 7 or above?
How to read file in Java?
How to read file using FileInputStream in Java?
How to read/parse XML file in Java?
How to write file in Java?
How to append text to an existing file in Java?
Source Code (Java 1.6 or less)
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileExample { public static void main(String[] args) { /* Create object of File for source file */ File sourceFile = new File("D:\\Readme.txt"); /* Create object of File for destination file */ File destinationFile = new File("D:\\CopyReadme.txt"); /* Create object of FileInputStream */ FileInputStream objFileInputStream = null; /* Create object of FileOutputStream */ FileOutputStream objFileOutputStream = null; /* Check if source file is exists or not */ if (sourceFile.exists()) { try { /* Obtain FileInputStream of source file */ objFileInputStream = new FileInputStream(sourceFile); /* Create destination file if not exists */ if (!destinationFile.exists()) { destinationFile.createNewFile(); } /* Obtain FileOutputStream of destination file */ objFileOutputStream = new FileOutputStream(destinationFile); /* Read content from source File. */ int byteOfData; while ((byteOfData = objFileInputStream.read()) != -1) { /* Write content to destination File. */ objFileOutputStream.write(byteOfData); } System.out.println("File Copied!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { /* Close FileInputStream and FileOutputStream */ try { if (objFileInputStream != null) { objFileInputStream.close(); } if (objFileOutputStream != null) { objFileOutputStream.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } else { System.out.println("Source file not found!"); } } }
Source Code (Java 1.7 or above)(try-with-resources
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyFileExample { public static void main(String[] args) { /* Create object of File for source file */ File sourceFile = new File("D:\\Readme.txt"); /* Create object of File for destination file */ File destinationFile = new File("D:\\CopyReadme.txt"); /* Check if source file is exists or not */ if (sourceFile.exists()) { /* Create destination file if not exists */ if (!destinationFile.exists()) { try { destinationFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try ( /* Create object of FileInputStream */ FileInputStream objFileInputStream = new FileInputStream(sourceFile); /* Create object of FileOutputStream */ FileOutputStream objFileOutputStream = new FileOutputStream(destinationFile); ) { /* Read content from source File. */ int byteOfData; while ((byteOfData = objFileInputStream.read()) != -1) { /* Write content to destination File. */ objFileOutputStream.write(byteOfData); } System.out.println("File Copied!"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } } }
Other References:
What is try-with-resources in Java 7 or above?
How to read file in Java?
How to read file using FileInputStream in Java?
How to read/parse XML file in Java?
How to write file in Java?
How to append text to an existing file in Java?
Tags:
FileInputStream
,
FileOutputStream
0 comments :