Problem : How you can make copy of a binary file?
Solution : java.io.FileInputStream will be used to read data from source binary file and java.io.FileOutStream will be used to write data to target binary file. Program :Step 1 - CodingCreate a text file c:/sunilos/CopyBinary.java and copy below contents.import java.io.FileInputStream; import java.io.FileOutputStream; /** * A program to copy a binary file to another file. */ public class CopyBinary { public static void main(String[] args) throws Exception { String source = "c:/sunilos/sunilos1.jpg"; String target = "c:/sunilos/sunilos.jpg"; FileInputStream reader = new FileInputStream(source); FileOutputStream writer = new FileOutputStream(target); int ch = reader.read(); while(ch!= -1){ writer.write(ch); ch = reader.read(); } writer.close(); reader.close(); System.out.println(source + " is copied " + target); } } Step 2 - Deployment
Step 3 - Testing
OutputC:\sunilos>java -cp . com.sunilos.io.CopyBinaryc:/sunilos/sunilos1.jpg is copied c:/sunilos/sunilos.jpg FAQWhat is FileInputStream?
What is FileOutputStream?
|