About Me

Age:25 --- B.E.(Computer Science), Android/Java Developer, CCNA, CCNA SECURITY (IINS), CCNP (R&S), 4011 Recognized(NSA & CNSS)U.S.A. , MCSA, MCTS, REDHAT CERTIFIED NETWORK SECURITY ADMINISTRATOR(RH253), AFCEH.

Monday, May 13, 2013

       

    //---> Writing Binary Files:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class WritingBinaryFiles {

 public boolean writeBinaryFile(File f) {

  boolean isSuccessful = true; // local variables in Java must be
         // initialized before being used

  try {

   FileInputStream fis = new FileInputStream(f);

   BufferedInputStream bis = new BufferedInputStream(fis);

   FileOutputStream fos = new FileOutputStream("e:\\new_file.png");

   BufferedOutputStream bos = new BufferedOutputStream(fos);

   int read = 0;

   while ((read = bis.read()) != -1) {

    bos.write(read);

   }

   bos.close();

   bis.close();

   isSuccessful = true;

  } catch (Exception e) {

   isSuccessful = false;

   e.printStackTrace();

  }

  return isSuccessful;

 }

 public static void main(String[] args) {

  File f = new File("e:\\a.png");

  boolean isDone = new WritingBinaryFiles().writeBinaryFile(f);

  if (isDone == true) {

   System.out.println("File successfully written");

  }

  else {

   System.out.println("File writing failed");

  }

 }

}
            
       
 

No comments:

Post a Comment